Qt Design Patterns

4,316 views
3,919 views

Published on

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

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,316
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
68
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Qt Design Patterns

  1. 1. C++/Qt DesignPatternsYnon Perekynon@ynonperek.comhttp://ynonperek.comSunday, May 5, 13
  2. 2. Good Code Bad CodeSunday, May 5, 13
  3. 3. When OOGoes WrongSunday, May 5, 13
  4. 4. Java InputValidation• In the past, Java Swingprovided only JTextField forinput texts• JNumericTextField quicklyappeared by the communitySunday, May 5, 13
  5. 5. Java InputValidationSunday, May 5, 13
  6. 6. OO Fail• Too many classes• Hard to maintain• Frustrated DevelopersSunday, May 5, 13
  7. 7. Enter Design Patterns• Reusable and tested solutions• Sustainable for the long runSunday, May 5, 13
  8. 8. Enter Design Patterns• Started by ChristopherAlexander in 1977• And he was an ArchitectSunday, May 5, 13
  9. 9. Example: Street Caffe“The street cafeprovides a uniquesetting, special tocities: a place wherepeople can sit lazily,legitimately, be onview, and watch theworld go by...”Sunday, May 5, 13
  10. 10. Patterns Are Not• Not a detailed solution• Not a guide to help you build itSunday, May 5, 13
  11. 11. Patterns Are• Describing existing and working solutions• Your job: Apply to yourworldSunday, May 5, 13
  12. 12. Patterns In Software• Defined by GoF• Categorized:• Creational• Behavioral• StructuralSunday, May 5, 13
  13. 13. Using Patterns• Understand• Memorize• Apply RepeatedlySunday, May 5, 13
  14. 14. Patterns In Qt• Plenty !• We’ll show 2:• Strategy• ProxySunday, May 5, 13
  15. 15. Strategy PatternSunday, May 5, 13
  16. 16. The Problem• An input text provides different validationtechniques• Only one is used at any given timeSunday, May 5, 13
  17. 17. Approach #1Sunday, May 5, 13
  18. 18. Why Is It Bad• Class Explosion• Hard to reuse validation codeSunday, May 5, 13
  19. 19. The Better WaySunday, May 5, 13
  20. 20. Why Is It Awesome• Can reuse validators code• Reduce class explosionSunday, May 5, 13
  21. 21. The Pattern• A validator encapsulates the validationalgorithm• The general pattern is called StrategySunday, May 5, 13
  22. 22. Intent• Define a family of algorithms, encapsulate eachone, and make them interchangeable.Sunday, May 5, 13
  23. 23. Motivation• Client code is simpler if the algorithm isexternal to it• No need to support an algorithm we don’tuse• We need to make it easy to add newalgorithmsSunday, May 5, 13
  24. 24. Applicability• Configure a class with one of manybehaviors• Use different variants of an algorithm• Eliminate Switch blocksSunday, May 5, 13
  25. 25. StructureSunday, May 5, 13
  26. 26. Participants• Strategy• ConcreteStrategy• ContextSunday, May 5, 13
  27. 27. Collaborations• Strategy + Context = Algorithm• Context forwards requests to StrategySunday, May 5, 13
  28. 28. Consequences• Families of related algorithms• Eliminate switch blocks• Allow different implementations• Strategy and Context interface can getcomplicated• Increased number of objectsSunday, May 5, 13
  29. 29. More Examples• Layout management• QAbstractNetworkCacheSunday, May 5, 13
  30. 30. Keep In Mind• A Default Strategy will make everyonehappy• Allow clients to create their own strategiesand pass as parametersSunday, May 5, 13
  31. 31. DemoSunday, May 5, 13
  32. 32. Q & ASunday, May 5, 13
  33. 33. Proxy PatternSunday, May 5, 13
  34. 34. ______________< Lets do COW >-------------- ^__^ (oo)_______(__) )/||----w ||| ||Sunday, May 5, 13
  35. 35. Example CodeQString s1 = getText();QString s2 = getText();QString s3 = s1;// "Hello World" is kept in memory only onces1.append(" And now its copied");Sunday, May 5, 13
  36. 36. The Proxy Way• A QString is just a proxy to the data• Implicitly sharedSunday, May 5, 13
  37. 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 WaySunday, May 5, 13
  38. 38. Proxy Types• Virtual Proxy• Protection Proxy• Remote ProxySunday, May 5, 13
  39. 39. Proxy: Pattern DetailsSunday, May 5, 13
  40. 40. Intent• Provide a surrogate or placeholder for anotherobjectSunday, May 5, 13
  41. 41. Motivation• Save time by lazy loading heavy resources• Easy CowsSunday, May 5, 13
  42. 42. Applicability• Access remote resources• Create resources on demand• Enforce per-object access controlSunday, May 5, 13
  43. 43. StructureSunday, May 5, 13
  44. 44. Participants• Proxy• Subject• Real SubjectSunday, May 5, 13
  45. 45. Collaborations• Proxy forwards requests to Real SubjectSunday, May 5, 13
  46. 46. Consequences• Hide the fact that an object is remote• Optimize load times (virtual proxy)• Can use for Copy-On-WriteSunday, May 5, 13
  47. 47. Qt Cow• Not really “By The Book”• QString has a Data struct• Does all work by itselfSunday, May 5, 13
  48. 48. Qt Patterns• Patterns are part of any framework (QtIncluded)• Understanding patterns helps us betterunderstand the frameworkSunday, May 5, 13
  49. 49. Thanks ForListening !Slides at:http://ynonperek.comQuestions / Comments:ynon@ynonperek.comStock Photos from:http://123rf.comSunday, May 5, 13
  50. 50. Thanks For ListeningSunday, May 5, 13

×