Design Patterns help us solve problems in tried and tested ways.
Turns out they also help us understand our framework better, for framework developers also use patterns.
In these slides you'll see how Design Patterns are implemented by Qt framework, to better both understand patterns and Qt
4. Java InputValidation
• In the past, Java Swing
provided only JTextField for
input texts
• JNumericTextField quickly
appeared by the community
Sunday, May 5, 13
6. OO Fail
• Too many classes
• Hard to maintain
• Frustrated Developers
Sunday, May 5, 13
7. Enter Design Patterns
• Reusable and tested solutions
• Sustainable for the long run
Sunday, May 5, 13
8. Enter Design Patterns
• Started by Christopher
Alexander in 1977
• And he was an Architect
Sunday, May 5, 13
9. Example: Street Caffe
“The street cafe
provides a unique
setting, special to
cities: a place where
people can sit lazily,
legitimately, be on
view, and watch the
world go by...”
Sunday, May 5, 13
10. Patterns Are Not
• Not a detailed solution
• Not a guide to help you build it
Sunday, May 5, 13
11. Patterns Are
• Describing existing and working solutions
• Your job: Apply to your
world
Sunday, May 5, 13
12. Patterns In Software
• Defined by GoF
• Categorized:
• Creational
• Behavioral
• Structural
Sunday, May 5, 13
20. Why Is It Awesome
• Can reuse validators code
• Reduce class explosion
Sunday, May 5, 13
21. The Pattern
• A validator encapsulates the validation
algorithm
• The general pattern is called Strategy
Sunday, May 5, 13
22. Intent
• Define a family of algorithms, encapsulate each
one, and make them interchangeable.
Sunday, May 5, 13
23. Motivation
• Client code is simpler if the algorithm is
external to it
• No need to support an algorithm we don’t
use
• We need to make it easy to add new
algorithms
Sunday, May 5, 13
24. Applicability
• Configure a class with one of many
behaviors
• Use different variants of an algorithm
• Eliminate Switch blocks
Sunday, May 5, 13
28. Consequences
• Families of related algorithms
• Eliminate switch blocks
• Allow different implementations
• Strategy and Context interface can get
complicated
• Increased number of objects
Sunday, May 5, 13
34. ______________
< Let's do COW >
--------------
^__^
(oo)_______
(__) )/
||----w |
|| ||
Sunday, May 5, 13
35. Example Code
QString s1 = getText();
QString s2 = getText();
QString s3 = s1;
// "Hello World" is kept in memory only once
s1.append(" And now it's copied");
Sunday, May 5, 13
36. The Proxy Way
• A QString is just a proxy to the data
• Implicitly shared
Sunday, May 5, 13
37. class MyCow
{
public:
MyCow &operator=(const MyCow &other)
{
m_data = other.m_data;
}
void write()
{
m_data = new Data( m_data );
m_data.write();
}
private:
Data *m_data;
};
The Proxy Way
Sunday, May 5, 13