design patterns
play

Design Patterns: History, Theory and Practice Drs. Vadim V. Zaytsev - PowerPoint PPT Presentation

Design Patterns: History, Theory and Practice Drs. Vadim V. Zaytsev 7 September 2004 Technical questions This course is given by Dr.ing. Ralf L ammel. All lectures are given in English. Ask small questions preferably during the


  1. Design Patterns: History, Theory and Practice Drs. Vadim V. Zaytsev 7 September 2004

  2. Technical questions • This course is given by Dr.ing. Ralf L¨ ammel. • All lectures are given in English. • Ask small questions preferably during the lecture, big ones in the break or via e-mail. • Up-to-date information about the course: rescheduling issues, slides, papers . . . — http://www.cs.vu.nl/~ralf/oo/lecture-2004/ • There will be a lecture by Ralf on the 14 th and no lecture on the 21 st of September. • These slides incorporate some of the work by Ralf L¨ ammel, Mehmet Ak¸ sit, Brian Malloy and Lodewijk Bergmans. 1

  3. What is a design pattern ? 2

  4. Background: brief history • First step: object-oriented programming languages (1982). • classes and objects as first-class entities • insufficient for creating object-oriented software • Second step: object-oriented methods (1988). • explore “real-world” domain concepts and apply object- oriented modelling techniques • did not make design knowledge explicit • Third step: object-oriented design patterns (1992). 3

  5. Discovering object-oriented design patterns • Determine the basic building blocks: • “these are the object-oriented concepts” Abstract Product A • Find a pattern for the problem: Abstract Factory createProductA()* createProductB()* Product A 2 Product A 1 Abstract Product B Concrete Factory 1 Concrete Factory 2 • “this is the pattern” createProductA() createProductA() createProductB() createProductB() Product B 2 Product B 1 • Record the pattern: • “this is the definition” 4

  6. Applying design patterns • Face a problem and search for a pattern: • “this is my problem” • Understand the pattern: • “this is the suitable pattern” • Implement solution according to the pattern: MASTER LIST PTR PTR DATA NIL DATA DATA DATA DATA DATA DATA DATA DATA NIL DATA DATA DATA NIL NIL • “this is the implementation” * DATA NIL 5

  7. Design patterns • Design pattern is a named abstraction for a recurring solution to a particular design problem. • Patterns are used to represent design knowledge to solve a particular type of problems. • The concept is due to Christopher Alexander (1979), now mostly by the book of Erich Gamma et al (“Gang’o’Four”, 1995) — explores basic object-oriented model features. • Patterns have explicit structure. 6

  8. Why one should use design patterns? • A language everyone speaks • Design language, independent of a programming language • Most design elements are covered by patterns • Patterns make design resistant to re-design. . . 7

  9. Robustness to change • Algorithmic dependencies: Template Method, Visitor, It- erator, Builder, Strategy. • Creating an object by specifying a class explicitly: Abstract Factory, Factory Method, Prototype. • Dependence on specific operations: Command, Chain of responsibility. • Dependence on object representation or implementation: Abstract Factory, Bridge, Proxy. 8

  10. Robustness to change Abstract Factory, Command, Fa¸ cade, • Tight coupling: Mediator, Observer, Bridge. Command, • Extending functionality by subclassing: Bridge, Composite, Decorator, Observer, Strategy. • Inability to alter classes conveniently: Visitor, Adapter, Decorator. 9

  11. Where and how do they arise? “Patterns are discovered, not invented” Richard Helm • 23 mostly used patterns come from the GoF book • lots of domain/language/. . . specific patterns exist • what is so difficult? • encapsulation • flexibility, extensibility • adaptability, ease of modification • performance • evolution • reusability • . . . 10

  12. Patterns classification by purpose purpose reflects what a pattern does • creational — facilitate object creation • structural — help to compose classes or objects — introduce ways objects interact and • behavioural distribute responsibility 11

  13. Patterns classification by scope scope specifies if the pattern applies to classes or objects deal with relationships between classes • class patterns or subclasses established through inheritance, while these relationships are fixed at compile time • objects patterns deal with object relationships that can be changed at runtime 12

  14. How to write down patterns? • Portland form — narrative unstructured form • Alexandrian form — problem, forces, solution • GoF-1 form — name, problem, solution, consequences • GoF-2 form — name, classification, intent, aka, motivation, applicability, structure, participants, collaborations, consequences, implementation, sample code, known uses, related patterns • No perfect form (yet). 13

  15. How to choose a pattern • Formulate your problem well • Know the basic catalogue • Scan the “intent” section • Study related patterns • Always have redesign case in mind 14

  16. Common problems • Getting the right pattern (even within those with the same UML diagram) • Taming over-enthusiasm (use all the patterns in one application; use only the pattern last learned) • Remember: the client does not give a damn about design patterns • Do not forget your own head 15

  17. Pattern example: Bridge (p.151) to avoid permanent binding between • Motivation: abstraction and implementation make the method ⇒ implementation an object itself! Implementation 1 Implementation 2 16

  18. Bridge: solution in UML Abstraction Implementation operation() implementedOper() Refined Abstraction Refined Implementation operation() implementedOper() 17

  19. Bridge: features • Easy to switch to an alternative implementation • Both parts are extensible • Implementation details are invisible for clients • All methods should be declared at the interface: no run-time extension • Loss of state in the implementation: working & switching When to use: different GUIs 18

  20. Pattern example: State (p.305) • Motivation: state-dependent behaviour ⇒ we should be able to change state at a run-time! • Alternative ways: FSM is usually programmed with a table or with (nested) conditional statements. The first method is bad if the table is sparse, the second is far from efficient (and maintainable) if complex. 19

  21. State: solution in UML Context State request() handle()* Concrete State A Concrete State B handle() handle() 20

  22. State: features • Localises state-specific behaviour and treat each state separately • Makes state transitions explicit (may be perfect, may be awful) • State objects can be shared ( static , fixed , whatever) • Creating and destroying state objects can be nasty • When to use: TCP protocol 21

  23. Pattern example: Strategy (p.315) dynamically changing algorithms (there are • Motivation: classes which differ only in their behaviour) • Different variants of an algorithm are needed A • Solution: make the algorithm an object! 22

  24. Strategy: solution in UML Context Algorithm contextInterface() algorithmInterface()* Concrete Strategy A Concrete Strategy B algorithmInterface() algorithmInterface() 23

  25. Strategy: features • Families of related algorithms • Alternative to subclassing • Choice of implementations of the same behaviour • All methods have to be declared explicitly • Overhead in communication; in the number of objects • When to use: mail composer 24

  26. What is going on? 25

  27. Things to be inferred: • There are different design patterns with very similar solutions. • Please recall: design patterns are pieces of knowledge, not just class diagrams. • It makes right design pattern choice even more challenging. • However, not all design patterns are about making new classes outta the old ones’ parts! 26

  28. Pattern example: Abstract Factory (p.87) independent creation and utilisation of prod- • Motivation: ucts. • “I know how to put objects together, I do not know how to make them” • Multiple families of products may be used by the same client, who is able to switch between factories. • Related products should be used together. 27

  29. Abstract Factory: solution in UML Abstract Product A Abstract Factory createProductA()* createProductB()* Product A 2 Product A 1 Abstract Product B Concrete Factory 1 Concrete Factory 2 createProductA() createProductA() createProductB() createProductB() Product B 2 Product B 1 28

  30. Abstract Factory: features • Isolates concrete classes • Makes exchanging product families easy • Promotes consistency among products • Only one instance of the factory is need (most often) • Creating new products: new subclass for each family • Creating new families: recompilation • A new factory is needed for any consistent composition • When to use: interface themes/skins 29

  31. Pattern example: Factory Method (p.107) inheritance-based design pattern for creating • Motivation: objects • A class should not know the class of objects it creates! • “I do not know the exact products, but I can explain how to do something with them” • The same knowledge can be used to put other objects together. 30

  32. Factory Method: solution in UML Creator factoryMethod()* operation() Abstract Product Concrete Creator Concrete Product factoryMethod() 31

Recommend


More recommend