1 / 51

Design Patterns

Design Patterns. Alan Shalloway, James Trott, Design Patterns Explained , Addison-Wesley, 2002. Gamma, Helm, Johnson, Vlissides, Design Patterns, Elements of Reusable Object-Oriented Software, Addison-Wesley, 1995. Perspectives in software development:.

blaze-rowe
Download Presentation

Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Patterns Alan Shalloway, James Trott, Design Patterns Explained, Addison-Wesley, 2002. Gamma, Helm, Johnson, Vlissides, Design Patterns, Elements of Reusable Object-Oriented Software, Addison-Wesley, 1995.

  2. Perspectives in software development: • Conceptual: represent concepts. Ignore how software implements it. (SRS) • Specification: look at the software interfaces, not at the implementation • Implementation: write code

  3. Design Pattern • Describes • problem that occurs repeatedly • the core of the solution to that problem • Solution can be reused • not necessary to implement it the same way

  4. Where do you see Design Patterns? • Seminars and conferences. • Technical journals. • Text books and trade journals. • You don’t really know OO until you understand these. • They may help you understand OO better.

  5. Where did they come from? Patterns start with a premise and answer two questions: • Premise: the goodness of a design is objective: not entirely in the eyes of the beholder. • Q1: What is present in good designs that is not present in poor ones? • Q2: What is present in poor designs that is not present in good ones?

  6. Pattern: A solution to a problem in a context • Name • Becomes part of our vocabulary • Allows abstract discussion of problem and approaches • Allows discussion of issues instead of implementation • Purpose • Description of the desirable thing • Problems that prevent the desirable solution explicitly described

  7. Vocabulary Example: find number in sorted list • Find middle value and compare value we are looking for to that one. If found, we’re done. Otherwise, if we are looking for something bigger, set the new bottom to the current middle. Otherwise, set the new top to the current middle. • Repeat the above step until the list can’t be divided further. If it’s not in the last two positions, it’s not in the list.

  8. Example 2: find number in sorted list • Use binary search.

  9. Example 2: find number in sorted list • Use binary search. • This assumes that you know what binary search is. • But if you know what binary search is, the discussion is greatly simplified. • Algorithms are not patterns. This is an analogy.

  10. Description of patterns: • Name • Intent • Problem • Solution how does the pattern solve the problem • Participants • Consequences investigate forces at play • Implementation Note: this is not the pattern itself. • References (GoF?)

  11. Elements • Pattern name • Problem • Explains the problem and its context • may include a list of conditions needed before application of solution makes sense • Solution • Consequences

  12. Elements • Pattern name • Problem • Solution • Describes elements of design, relationships, responsibilities, and collaborations • Does not describe a particular implementation • Abstract description and how a general arrangements of objects solves problem • Consequences

  13. Elements • Pattern name • Problem • Solution • Consequences • Results and trade-offs of applying pattern • Needed to evaluate tradeoffs between solutions • Frequently time/space trade-offs

  14. Why patterns? • Reuse existing, high-quality solutions to common problems • incorporates a great deal of experience • may describe things often overlooked or subtle that prevent good solutions

  15. Why patterns? • Reuse existing, high-quality solutions to common problems • incorporates a great deal of experience • may describe things often overlooked or subtle that prevent good solutions • Focus on issues not implementation • Shift thinking to higher level • Decide if design is right, not just working • Learn how to design • Improve code: smaller hierarchies, more modifiable code.

  16. Strategies suggested by patterns • Design to interfaces

  17. Strategies suggested by patterns • Design to interfaces • Favor composition over inheritance

  18. Strategies suggested by patterns • Design to interfaces • Favor composition over inheritance • Find what varies and encapsulate it. • Consider what should vary in your design. • Consider what you want to be able to change without redesign (as opposed to what might force you to redesign) • Encapsulate this

  19. Catalog of Patterns • GOF = “Gang of four” (1995) • Erich Gamma • Richard Helm • Ralph Johnson • John Vlissides • 23 patterns • Others since then

  20. Abstract Factory Adapter Bridge Builder Chain of Responsibility Command Composite Decorator Facade Factory Method Flyweight Interpreter Iterator Mediator Memento GOF: 23 Patterns • Observer • Prototype • Proxy • Singleton • State • Strategy • Template Method • Visitor

  21. Design Pattern Classification • We classify design patterns by two criteria: • Purpose: Reflects what the Pattern does. • Scope: Specifies whether the pattern applies primarily to classes or to objects.

  22. Design Pattern Purposes • Patterns can have either creational, structural, or behavioral purpose. • Creational patterns: concern the process of object creation. • Structural patterns: deal with the composition of classes or objects. • Behavioral patterns: characterize the ways in which classes or objects interact and distribute responsibility.

  23. Design Patterns Scopes • Class patterns deal with relationships between classes and their subclasses. • These relationships are established through inheritance, so they are static—fixed at compile-time. • Object patterns deal with object relationships, which can be changed at run-time and are more dynamic.

  24. In General • Almost all patterns use inheritance to some extent. • The only patterns labeled "class patterns" are those that focus on class relationships. • Note that most patterns are in the Object scope.

  25. Solving Problems • Patterns help you identify non-obvious abstractions • e. g. Strategy describes pattern for interchangeable families of algorithms • Patterns can guide decisions about what is an object

  26. How to Select a Design Pattern • Consider how design patterns solve design problems • Scan Intent sections • Study how patterns interrelate and patterns of like purpose • Consider what should be variable in your design

  27. How to use a pattern • Read the pattern paying attention to Applicability and Consequences • Study the Structure, Participants, and Collaborations • Look at Sample Code • Choose good names for participants • Define the classes, interfaces, and operations • Implement the operations to carry out the responsibilities and collaborations

  28. Design Patterns: Model-View Separation

  29. Motivation: • Suppose you have a system that needs to run on a variety of devices • Maybe a cell phone and a PDA • Maybe Windows and with a command line • . . .

  30. Motivation: • Suppose we support both the command line and the GUI interfaces. • What changes? • What stays the same?

  31. Motivation: • Suppose we support both the command line and the GUI interfaces. • What changes? • What stays the same? • How do you design to handle this?

  32. Model-View Separation Pattern • Model: The domain layer of objects. (objects that contain data and operations). • View: The presentation layer of objects (windows, applets, reports).

  33. Model-View Context • Context/Problem • It is desirable to: • de-couple domain (model) objects from windows (views) • support increased reuse of domain objects • minimize the impact of changes in the interface upon domain objects

  34. Model-View Context • Context/Problem It is desirable to de-couple domain (model) objects from windows (views), to support increased reuse of domain objects, and minimize the impact of changes in the interface upon domain objects. • Solution Define the domain (model) classes so that they do not have direct coupling or visibility into the window (view) classes, and so that application data and functionality is maintained in domain classes, not window classes.

  35. Model-View Window View display( ) Goal: Classes in Model should not have direct visibility to classes in View. Configuration Model addEntry( ) query( )

  36. Model-View Separation Motivation Motivation • Focus more on the domain processes rather than on computer interfaces. • Allow separate development of the model and user interface layers. • Minimize the impact of changes in the interface upon the domain layer. • Allow new views to be easily connected to an existing domain layer.

  37. Problem Worse Window View Model classes talk to View classes. display( ) displayMessage ( ) Configuration Model addEntry( ) query( )

  38. Better View classes talk to Model classes. Model-View Separation Pattern Window View display( ) query( ) Configuration Model addEntry( ) query( )

  39. Old Window New Window display( ) display( ) Model-View Separation Pattern View query( ) The View Layer can be modified without affecting the Model layer. Configuration Model addEntry( ) query( )

  40. New Window display( ) Model-View Separation Pattern When the Window needs to display data it queries the Model objects “Polling Model” View query( ) Configuration Model addEntry( ) query( )

  41. Model-View Separation Pattern • Problem: Domain objects need to communicate with windows to cause a real-time ongoing display update as the state of information in the domain object changes. - Monitoring applications - Simulation applications • Solution: Indirect Visibility

  42. Model-View Separation Pattern with Indirect Visibility • Named Publish-Subscribe Pattern • Context/Problem: A change in state (an event) occurs within a Publisher of the event and other objects are dependant on or interested in this event (Subscribers to the event). The Publisher should not have direct knowledge of its subscribers. • Solution: Define an event notification system so that the Publisher an indirectly notify Subscribers. Event Manager or Model View Controller (MVC).

  43. Model-View Separation Pattern with Indirect Visibility http://gmoeck.github.com/2011/03/10/sproutcore-mvc-vs-rails-mvc.html

  44. Model-View Separation Pattern with Indirect Visibility

  45. Design Patterns:Façade

  46. Façade • Intent: want to simplify the use of an existing system. Need to define an interface that is a subset of the existing one. • Problem: • You only need a subset of a complex system or need to interact in a particular way. • The API for the subset is simpler than the API for the entire subsystem. • You may want to hide the original system, or you may want to use a subset of the system and add functionality.

  47. Façade • Solution: The façade presents a new interface • Participants: presents a specialized interface to the client to make it easier to use • Consequences: • Simplifies the use of the subsystem. • Some functionality may not be available to the client. • Implementation: • Define a new class (or classes) that has the required interface. • Have this class use the existing system.

  48. Façade Example • Huge database interface, but we only need a few of the functions. • Rather than everyone learning the database interface, create the interface you need, and build a façade.

  49. ClientA Database ClientB Element Model Without Façade

  50. ClientA Database ClientB Element Model DBFacade With Façade

More Related