Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Design patterns

6,796 views

Published on

A short introduction to design patterns with composite and 3-tier modeling. Note: its for system modeling rather than software engineering. No implementation is involved.

Design patterns

  1. 1. Session 3: Design Patterns Analysis and Specification of Information Systems Winter 2008 Eran Toch http://www.technion.ac.il/~erant Specification and Analysis of Information Systems Spring 2005 1
  2. 2. Agenda • What are Design Patterns? • Façade Pattern • 3-Tier Pattern • Composite Pattern • Go Back to a Safe Place Pattern • Summary Intro | Façade | 3-Tier | Composite | Safe Place | Summary 2
  3. 3. Patterns in Architecture • Does this room makes you feel happy? • Why? – Light (direction) – Proportions – Symmetry – And more... Intro | Façade | 3-Tier | Composite | Safe Place | Summary 3
  4. 4. What is a Design Pattern? A description of a recurrent problem and of the core of possible solutions In short, a reference Intro | Façade | 3-Tier | Composite | Safe Place | Summary 4
  5. 5. Why do we need Patterns? • Reusing design knowledge – Problems are not always unique. Reusing existing experience might be useful. – Patterns give us hints to “where to look for problems”. • Establish common terminology – Easier to say, "We need a Façade here“. • Provide a higher level of abstraction – Frees us from dealing with the details too early Intro | Façade | 3-Tier | Composite | Safe Place | Summary 5
  6. 6. History of Design Patterns Christopher Alexander The Timeless Way of Building Architecture 1970’ A Pattern Language: Towns, Buildings, Construction Gang of Four (GoF) Object Oriented Design Patterns: Elements of 1995’ Reusable Object-Oriented Software Software Design Other Areas: Many Authors HCI, Organizational Behavior, 2007’ Education, Concurent Programming… Intro | Façade | 3-Tier | Composite | Safe Place | Summary 6
  7. 7. Structure of a design pattern* • Pattern Name and Classification • Intent – a Short statement about what the pattern does • Motivation – A scenario that illustrates where the pattern would be useful • Applicability – Situations where the pattern can be used *According to GoF Intro | Façade | 3-Tier | Composite | Safe Place | Summary 7
  8. 8. Structure – cont’d • Structure – A graphical representation of the pattern • Participants – The classes and objects participating in the pattern • Collaborations – How to do the participants interact to carry out their responsibilities? • Consequences – What are the pros and cons of using the pattern? • Implementation – Hints and techniques for implementing the pattern Intro | Façade | 3-Tier | Composite | Safe Place | Summary 8
  9. 9. Classification of GoF Patterns Taken from Vince Huston’s site about Design Patterns http://home.earthlink.net/~huston2/dp/patterns.html Intro | Façade | 3-Tier | Composite | Safe Place | Summary 9
  10. 10. Agenda • What are Design Patterns? • Façade Pattern • 3-Tier Pattern • Composite Pattern • Go Back to a Safe Place Pattern • Summary Intro | Façade | 3-Tier | Composite | Safe Place | Summary 10
  11. 11. Façade – an Example • A robot has four classes: – Camera (that can identify objects) – Arm (that can move) – Pillars (that can grab) – Operator – managing other classes Subsystem: mechanics Intro | Façade | 3-Tier | Composite | Safe Place | Summary 11
  12. 12. Problems 1. How much does the operator needs to know regarding the mechanics? 2. For instance, we want to identify an object and move it to some predefined location. The code would look like: oldLocation = Camera.identify(object); Arm.move(oldLocation); Pliers.close(); Arm.move(newLocation); Pliers.open(); • Problem: No encapsulation – Operator needs to know a lot: structure + behavior – Preconditions – Agility Intro | Façade | 3-Tier | Composite | Safe Place | Summary 12
  13. 13. The Façade Pattern Intro | Façade | 3-Tier | Composite | Safe Place | Summary 13
  14. 14. Façade - Definition • Intent – Provide a unified interface to a set of interfaces in a subsystem. – Façade defines a higher-level interface that makes the subsystem easier to use. • Applicability – Use the Façade pattern • to provide a simple, default view of a complex subsystem. • to layer your subsystems. Use a façade as the entry point to each layer. Intro | Façade | 3-Tier | Composite | Safe Place | Summary 14
  15. 15. Façade Example Operators interacts only with the Façade class The Façade Class “knows” and controls the subsystem objects Example Intro | Façade | 3-Tier | Composite | Safe Place | Summary 15
  16. 16. Façade – the System Level • Encapsulation at higher levels: – General patterns for constructing subsystems. – Each subsystem is represented by a façade interface – Inner details are encapsulated Intro | Façade | 3-Tier | Composite | Safe Place | Summary 16
  17. 17. Façade - Consequences • Good: – Shields clients from subsystem components – It promotes weak coupling between the subsystem and its clients. – Façades help layer a system • Bad: – Duplicated methods – Lower visibility of subsystem functionality Intro | Façade | 3-Tier | Composite | Safe Place | Summary 17
  18. 18. More Structural Patterns • Adapter - Match interfaces of different classes • Bridge - Separates an object’s interface from its implementation • Decorator - Add responsibilities to objects dynamically • Flyweight - A fine-grained instance used for efficient sharing • Proxy - An object representing another object Intro | Façade | 3-Tier | Composite | Safe Place | Summary 18
  19. 19. Agenda • What are Design Patterns? • Façade Pattern • 3-Tier Pattern • Composite Pattern • Go Back to a Safe Place Pattern • Summary Intro | Façade | 3-Tier | Composite | Safe Place | Summary 19
  20. 20. Buying a Computer: Multiple Channels An e-commerce system: –Products –Customers –Orders Intro | Façade | 3-Tier | Composite | Safe Place | Summary 20
  21. 21. Managing Shopping Systems • Actions are specified twice • Adding new actions is difficult • Manageability – Different teams working on store/web etc... Example Intro | Façade | 3-Tier | Composite | Safe Place | Summary 21
  22. 22. 3-Tier Pattern • Intent – Separating application view, business logic and data • Structural Pattern – Can be expressed using specialized sterotypes in class diagram • Origin – By Ivar Jacobson (not GoF) – Based on the MVC (Model-View-Controller) pattern See: Rumbaugh et al, Object Oriented Modeling and Design, Prentice Hall, 1991. Intro | Façade | 3-Tier | Composite | Safe Place | Summary 22
  23. 23. 3-Tier Structure Represents the system’s interface to the outside world Represents application processes Represents consistent data This is a regular class diagram! Illustration Intro | Façade | 3-Tier | Composite | Safe Place | Summary 23
  24. 24. Step 1 – Boundary Tier • In the simplest form, each actor gets an interface: • However, it can be more complicated: – Dividing complex interfaces – Combining interfaces – Generalizing interfaces Intro | Façade | 3-Tier | Composite | Safe Place | Summary 24
  25. 25. Boundary Tier Member Distribution • Boundary tier objects are created when the session starts and are destroyed when the session ends. • Contains: – Methods operated by the user. – Attributes of the interface. Interface configuration Pull Push properties messages messages Intro | Façade | 3-Tier | Composite | Safe Place | Summary 25
  26. 26. Step 2 – Control Tier We add a control tier. Basically, every use-case gets a control class that handles it’s process Controls can be decomposed into sub- processes. Again, inheritance can be used Intro | Façade | 3-Tier | Composite | Safe Place | Summary 26
  27. 27. Control Tier Member Distribution • Control objects are created when the action (use-case) starts and are destroyed when it ends • Contain mainly methods, some of them double the ones in the boundary tier objects • May contain attributes that are used for the process’s flow management Process state Flow management of variables other control objects Calling interface objects Intro | Façade | 3-Tier | Composite | Safe Place | Summary 27
  28. 28. Step 3 – Entity Tier We add the Entity classes. These classes compose most of the classes in the regular class-diagram mode Entity classes have mainly associations, rather than dependencies, because they are consistent through the processes Intro | Façade | 3-Tier | Composite | Safe Place | Summary 28
  29. 29. Entity Tier Member Distribution • Entity tier objects represent the consistent data in the system. They are created and destroyed according to the processes that control them • Contain mainly attributes and some data-handling methods Example Intro | Façade | 3-Tier | Composite | Safe Place | Summary 29
  30. 30. 3 Tier Usage patterns “l” Structure “V” Structure “Y” Structure Used for date Used for communication Used for communication update / retrieve + update / retrieve Intro | Façade | 3-Tier | Composite | Safe Place | Summary 30
  31. 31. 3-Tier Consequences • Good: – Re-use of Model components – Flexible processes – Easier support for new types of views Transaction Info Loan executing viewing Manage • Bad: – Increased design complexity. Date Intro | Façade | 3-Tier | Composite | Safe Place | Summary 31
  32. 32. Behavioral Patterns • Chain of Responsibilities - A way of passing a request between a chain of objects • Command - Encapsulate a command request as an object • Interpreter - A way to include language elements in a program • Iterator - Sequentially access the elements of a collection • Mediator - Defines simplified communication between classes • Memento - Capture and restore an object's internal state Intro | Façade | 3-Tier | Composite | Safe Place | Summary 32
  33. 33. Behavioral Patterns – cont’d • Observer - A way of notifying change to a number of classes • State - Alter an object's behavior when its state changes • Strategy - Encapsulates an algorithm inside a class • Template Method - Defer the exact steps of an algorithm to a subclass • Visitor - Defines a new operation to a class without change Intro | Façade | 3-Tier | Composite | Safe Place | Summary 33
  34. 34. Agenda • What are Design Patterns? • Façade Pattern • 3-Tier Pattern • Composite Pattern • Go Back to a Safe Place Pattern • Summary Intro | Façade | 3-Tier | Composite | Safe Place | Summary 34
  35. 35. An Example Client Files Folders Intro | Façade | 3-Tier | Composite | Safe Place | Summary 35
  36. 36. A Solution? • Folder: – For each action (delete, display, copy etc), there is special treatment for files and folders. • Explorer: – Each type of object is manipulated separately • Scalability: – Handling larger Example number of elements (disks, CD, USB…) Intro | Façade | 3-Tier | Composite | Safe Place | Summary 36
  37. 37. The Composite Solution Example Intro | Façade | 3-Tier | Composite | Safe Place | Summary 37
  38. 38. Composite: Structure Illustration • An abstract base class (Component) specifies the uniform behavior. • Primitive and Composite classes are subclassed. • Composite Manages components uniformly, using add and remove. Intro | Façade | 3-Tier | Composite | Safe Place | Summary 38
  39. 39. Composite: Consequences • Good – It makes it easy to add new kinds of components. – It makes clients simpler. • Bad – It makes it harder to restrict the type of components of a composite. Intro | Façade | 3-Tier | Composite | Safe Place | Summary 39
  40. 40. Composite: Known Uses Graphical User Interface Arithmetic Expressions A * (B + (C * D)) * A () + B * C D Intro | Façade | 3-Tier | Composite | Safe Place | Summary 40
  41. 41. Agenda • What are Design Patterns? • Façade Pattern • 3-Tier Pattern • Composite Pattern • Go Back to a Safe Place Pattern • Summary Intro | Façade | 3-Tier | Composite | Safe Place | Summary 41
  42. 42. Human / Computer Interaction Patterns • Human / Computer Interaction is about how users interact with computers • Some interactions are recurrent and can be expressed by patterns: – Form – Control Panel – WYSIWYG Editor – Hierarchical Set – Map of Navigable Spaces – Go Back to a Safe Place – More… Intro | Façade | 3-Tier | Composite | Safe Place | Summary 42
  43. 43. Computers can be S C A R Y Sometimes an innocent user gets into a state she don’t want to be in… And then, the terror!!! Intro | Façade | 3-Tier | Composite | Safe Place | Summary 43
  44. 44. Solution • Provide a way to go back to a checkpoint of the user's choice. The "Home" button The “undo" feature and the “Back” Intro | Façade | 3-Tier | Composite | Safe Place | Summary 44
  45. 45. Agenda • What are Design Patterns? • Composite Pattern • Façade Pattern • 3-Tier Pattern • Go Back to a Safe Place Pattern • Summary Intro | Façade | 3-Tier | Composite | Safe Place | Summary 45
  46. 46. Summary  Patterns learned: – Façade, Composite, 3-Tier, Go Back to a Safe Place.  Advantages of Design Patterns: – They capture expertise and make it accessible to non- experts. – They form a vocabulary that helps developers communicate better. – They help people understand a systems more quickly when it is documented with the patterns it uses. Intro | Façade | 3-Tier | Composite | Safe Place | Summary 46
  47. 47. Relevancy to the Course • Most of the design patterns are relevant to the design and coding phases • However, some of them are very relevant as a tool for the analysis phase Chain of resp. Visitor Flyweight 3-Tier Façade Composite Singleton Prototype Analysis Design Intro | Façade | 3-Tier | Composite | Safe Place | Summary 47
  48. 48. References • Design Patterns: Elements of Reusable Object-Oriented Software, Gamma E. et el., 1995, Addison-Wesley. • The original patterns for architecture by C. Alexander http://www.enumerable.com/dev/leveltwo/patternsframe.htm?/dev/leveltwo • A course from Bob Tarr from UMBC University http://www.research.umbc.edu/~tarr/dp/fall00/cs491.html • The Design Patterns Java Companion, James W. Cooper (an online version of the book) http://www.patterndepot.com/put/8/JavaPatterns.htm • A Site dedicated to Design Patterns by Vince Huston http://home.earthlink.net/~huston2/dp/patterns.html Intro | Façade | 3-Tier | Composite | Safe Place | Summary 48
  49. 49. References – con’d • Seven Habits of Successful Pattern Writers, John Vlissides http://hillside.net/patterns/papers/7habits.html • COMMON GROUND: A Pattern Language for Human- Computer Interface Design, Jenifer Tidwell, http://www.mit.edu/~jtidwell/common_ground_onefile.html • Design patterns for .NET: http://www.dofactory.com/Patterns/Patterns.aspx • Patterns for Concurrent, Parallel, and Distributed Systems http://www.cs.wustl.edu/~schmidt/patterns-ace.html • J2EE Patterns Catalog http://java.sun.com/blueprints/patterns/catalog.html Intro | Façade | 3-Tier | Composite | Safe Place | Summary 49
  50. 50. Thanks! Specification and Analysis of Information Systems Spring 2005 50

×