C++/Qt Design Patterns Ynon Perek ynon@ynonperek.com http://ynonperek.com Sunday, May 5, 13
Good Code Bad Code Sunday, May 5, 13
When OO Goes Wrong Sunday, May 5, 13
Java InputValidation • In the past, Java Swing provided only JTextField for input texts • JNumericTextField quickly appeared by the community Sunday, May 5, 13
Java InputValidation Sunday, May 5, 13
OO Fail • Too many classes • Hard to maintain • Frustrated Developers Sunday, May 5, 13
Enter Design Patterns • Reusable and tested solutions • Sustainable for the long run Sunday, May 5, 13
Enter Design Patterns • Started by Christopher Alexander in 1977 • And he was an Architect Sunday, May 5, 13
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
Patterns Are Not • Not a detailed solution • Not a guide to help you build it Sunday, May 5, 13
Patterns Are • Describing existing and working solutions • Your job: Apply to your world Sunday, May 5, 13
Patterns In Software • Defined by GoF • Categorized: • Creational • Behavioral • Structural Sunday, May 5, 13
Using Patterns • Understand • Memorize • Apply Repeatedly Sunday, May 5, 13
Patterns In Qt • Plenty ! • We’ll show 2: • Strategy • Proxy Sunday, May 5, 13
Strategy Pattern Sunday, May 5, 13
The Problem • An input text provides different validation techniques • Only one is used at any given time Sunday, May 5, 13
Approach #1 Sunday, May 5, 13
Why Is It Bad • Class Explosion • Hard to reuse validation code Sunday, May 5, 13
The Better Way Sunday, May 5, 13
Why Is It Awesome • Can reuse validators code • Reduce class explosion Sunday, May 5, 13
The Pattern • A validator encapsulates the validation algorithm • The general pattern is called Strategy Sunday, May 5, 13
Intent • Define a family of algorithms, encapsulate each one, and make them interchangeable. Sunday, May 5, 13
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
Applicability • Configure a class with one of many behaviors • Use different variants of an algorithm • Eliminate Switch blocks Sunday, May 5, 13
Structure Sunday, May 5, 13
Participants • Strategy • ConcreteStrategy • Context Sunday, May 5, 13
Collaborations • Strategy + Context = Algorithm • Context forwards requests to Strategy Sunday, May 5, 13
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
More Examples • Layout management • QAbstractNetworkCache Sunday, May 5, 13
Keep In Mind • A Default Strategy will make everyone happy • Allow clients to create their own strategies and pass as parameters Sunday, May 5, 13
Demo Sunday, May 5, 13
Q & A Sunday, May 5, 13
Proxy Pattern Sunday, May 5, 13
______________ < Let's do COW > -------------- ^__^ (oo)_______ (__) )/ ||----w | || || Sunday, May 5, 13
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
The Proxy Way • A QString is just a proxy to the data • Implicitly shared Sunday, May 5, 13
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
Proxy Types • Virtual Proxy • Protection Proxy • Remote Proxy Sunday, May 5, 13
Proxy: Pattern Details Sunday, May 5, 13
Intent • Provide a surrogate or placeholder for another object Sunday, May 5, 13
Motivation • Save time by lazy loading heavy resources • Easy Cows Sunday, May 5, 13
Applicability • Access remote resources • Create resources on demand • Enforce per-object access control Sunday, May 5, 13
Structure Sunday, May 5, 13
Participants • Proxy • Subject • Real Subject Sunday, May 5, 13
Collaborations • Proxy forwards requests to Real Subject Sunday, May 5, 13
Consequences • Hide the fact that an object is remote • Optimize load times (virtual proxy) • Can use for Copy-On-Write Sunday, May 5, 13
Qt Cow • Not really “By The Book” • QString has a Data struct • Does all work by itself Sunday, May 5, 13
Qt Patterns • Patterns are part of any framework (Qt Included) • Understanding patterns helps us better understand the framework Sunday, May 5, 13
Thanks For Listening ! Slides at: http://ynonperek.com Questions / Comments: ynon@ynonperek.com Stock Photos from: http://123rf.com Sunday, May 5, 13
Thanks For Listening Sunday, May 5, 13

Qt Design Patterns