Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Design patterns

999 Aufrufe

Veröffentlicht am

Veröffentlicht in: Design, Technologie, Unterhaltung & Humor
  • Als Erste(r) kommentieren

Design patterns

  1. 1. Design Patterns – Eran TochMethodologies in Information System DevelopmentDesign Patterns:Design Patterns:Talking in the Design LanguageTalking in the Design LanguageEran TochMethodologies in the Developmentof Information Systems
  2. 2. Design Patterns – Eran Toch2November 2003AgendaAgenda• What is a Design Pattern?• Structural Patterns– The Composite Pattern• Behavioral Patterns– The Strategy Pattern• Creational Patterns– The Singleton Pattern• Human Interaction Patterns– Go Back to a Safe Place• Summary
  3. 3. Design Patterns – Eran Toch3November 2003What is a Design Pattern?What is a Design Pattern?In Short, a solutionfor a typical problem“a description of a recurrentproblem and of the core of possiblesolutions.“
  4. 4. Design Patterns – Eran Toch4November 2003Why do we need them?Why do we need them?• Problems are not always unique. Reusingexisting experience might be proved useful.• We need a common language to describeproblems and solutions.• The Design of Object-Oriented systems limitsthe scope of problems and solutions we cantalk about
  5. 5. Design Patterns – Eran Toch5November 2003History of Design PatternsHistory of Design PatternsChristopher AlexanderA Pattern Language: Towns, Buildings Construction 1970’1995’2000’ArchitectureObject OrientedSoftware DesignOther Areas:HCI, Organizational Behavior…Gang of Four (GoF)Design Patterns: Elements ofReusable Object-Oriented SoftwareMany Authors
  6. 6. Design Patterns – Eran Toch6November 2003Structure of a design patternStructure 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 beuseful• Applicability– Situations where the pattern can be used
  7. 7. Design Patterns – Eran Toch7November 2003Structure of a design pattern – cont’dStructure of a design pattern – 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 theirresponsibilities?• Consequences– What are the pros and cons of using the pattern?• Implementation– Hints and techniques for implementing the pattern
  8. 8. Design Patterns – Eran Toch8November 2003Classification of patternsClassification of patterns• Structural– Composite*, Decorator, Façade, Adapter…• Behavioral– Strategy*, Command, Observer, Chain ofResponsibilities…• Creational– Singleton*, Abstract Factory, Factory Method…• Other– User interface Patterns• Go Back to a Safe Place
  9. 9. Design Patterns – Eran Toch9November 2003AgendaAgenda• What is a Design Pattern?• Structural Patterns• Behavioral Patterns• Creational Patterns• Human Interaction Patterns• Summary
  10. 10. Design Patterns – Eran Toch10November 2003The Composite PatternThe Composite Pattern• Intent:– Compose objects into tree structures to representpart-whole hierarchies.– Composite lets clients treat individual objects andcompositions of objects uniformly. This is calledrecursive composition.
  11. 11. Design Patterns – Eran Toch11November 2003MotivationMotivationWhen Processing the hierarchalcollection, we need to query thetype of the object (composite orprimitive) and act differentlyDiagram reference: DesignPatterns [Gof]
  12. 12. Design Patterns – Eran Toch12November 2003Composite: ApplicabilityComposite: Applicability• Use the Composite pattern when:– You want to represent part-whole hierarchies ofobjects.– You want clients to be able to ignore thedifference between compositions of objects andindividual objects. Clients will treat all objects inthe composite structure uniformly.
  13. 13. Design Patterns – Eran Toch13November 2003Composite: StructureComposite: Structure• Define an abstract base class that specifies theuniform behavior.• Subclass the Primitive and Composite classes of theComponent class.Diagram reference: DesignPatterns [Gof]
  14. 14. Design Patterns – Eran Toch14November 2003Composite: ConsequencesComposite: Consequences• Benefits– It makes it easy to add new kinds of components– It makes clients simpler, since they do not have toknow if they are dealing with a leaf or a compositecomponent• Liabilities– It makes it harder to restrict the type of components ofa composite
  15. 15. Design Patterns – Eran Toch15November 2003Composite: Known UsesComposite: Known Uses1. A GUI system has window objects which cancontain various GUI components (widgets) such as,buttons and text areas. A window can also containwidget container objects which can hold otherwidgets.Concrete examples: Java Swing, MFC2. Folder based file system3. Arithmetic expressions
  16. 16. Design Patterns – Eran Toch16November 2003AgendaAgenda• What is a Design Pattern?• Structural Patterns• Behavioral Patterns• Creational Patterns• Human Interaction Patterns• Summary
  17. 17. Design Patterns – Eran Toch17November 2003The Strategy PatternThe Strategy Pattern• Intent:– Define a family of algorithms, encapsulate each one,and make them interchangeable.– Strategy lets the algorithm vary independently fromclients that use it.
  18. 18. Design Patterns – Eran Toch18November 2003Strategy: MotivationStrategy: MotivationThe client wants to use the algorithmwithout knowing which one to use inadvance. We want to add newalgorithms without changing theclient class.
  19. 19. Design Patterns – Eran Toch19November 2003Strategy: StructureStrategy: Structure• Encapsulate the behavior of the context class in aseparate algorithm class.• The context class keeps a reference to the algorithmclass and passes on the messages.• The algorithm class can be sub-classed to supportmultiple algorithms.
  20. 20. Design Patterns – Eran Toch20November 2003Strategy: ConsequencesStrategy: Consequences• Benefits– Provides an alternative to subclassing the Contextclass to get a variety of algorithms or behaviors– Keeps the client clean from large conditionalstatements– Provides a choice of implementations for the samebehavior• Liabilities– Increases the number of objects– All algorithms must use the same Strategy interface– We still need conditional statements!
  21. 21. Design Patterns – Eran Toch21November 2003Strategy: Known UsesStrategy: Known Uses• QSIA Learning System• Layout Manager in JavaSwing• Sorting algorithms,memory allocationalgorithms and more…
  22. 22. Design Patterns – Eran Toch22November 2003AgendaAgenda• What is a Design Pattern?• Structural Patterns• Behavioral Patterns• Creational Patterns• Human Interaction Patterns• Summary
  23. 23. Design Patterns – Eran Toch23November 2003The Singleton PatternThe Singleton Pattern• Intent.– Ensure a class only has one instance, and provide aglobal point of access to it.• Motivation.– Sometimes, an application needs one, and only one,instance of an object. For example, we want just onewindow manager. Or just one factory for a family ofproducts.– We need to have that one instance easily accessible.– And we want to ensure that additional instances of theclass can not be created.
  24. 24. Design Patterns – Eran Toch24November 2003Singleton: StructureSingleton: Structure• Declare the single instance as a private static datamember.• Provide a public static member function thatencapsulates all initialization code, and providesaccess to the instance.
  25. 25. Design Patterns – Eran Toch25November 2003Singleton: Example CodeSingleton: Example Codepublic class Singleton {// The private reference to the one and only instance.private static Singleton uniqueInstance = null;// An instance attribute.private int data = 0;/*** Returns a reference to the single instance.* Creates the instance if it does not yet exist.*/public static Singleton instance() {if(uniqueInstance == null)uniqueInstance = new Singleton();return uniqueInstance;}/*** The Singleton Constructor. Note that it is private!* No client can instantiate a Singleton object!*/private Singleton() {}}
  26. 26. Design Patterns – Eran Toch26November 2003Singleton: Example Code – cont’dSingleton: Example Code – cont’dpublic class TestSingleton {public static void main(String args[]) {// Get a reference to the single instance of Singleton.Singleton s = Singleton.instance();// Set the data value.s.setData(34);System.out.println("First reference: " + s);System.out.println("Singleton data value is: " + s.getData());// Get another reference to the Singleton.// Is it the same object?s = null;s = Singleton.instance();System.out.println("nSecond reference: " + s);System.out.println("Singleton data value is: " + s.getData());}}What will be the output?
  27. 27. Design Patterns – Eran Toch27November 2003AgendaAgenda• What is a Design Pattern?• Structural Patterns• Behavioral Patterns• Creational Patterns• Human Interaction Patterns• Summary
  28. 28. Design Patterns – Eran Toch28November 2003Human Interaction PatternsHuman Interaction Patterns• A Pattern language to describe humaninteraction situations• Examples:– Go Back to a Safe Place– Form– Control Panel– WYSIWYG Editor– Hierarchical Set– Map of Navigable Spaces– More…
  29. 29. Design Patterns – Eran Toch29November 2003Go Back to a Safe PlaceGo Back to a Safe Place• Problem:– How can the artifact make navigation easy, convenient, andpsychologically safe for the user?• Forces:– A user may forget where they were, if they stop using the artifactwhile theyre in the middle of something and dont get back to itfor a while.– If the user gets into a space or a state that they dont want to bein, they will want to get out of it in a safe and predictable way.– The user is more likely to explore an artifact if they are assuredthat they can easily get out of an undesired state or space; thatassurance engenders a feeling of security.
  30. 30. Design Patterns – Eran Toch30November 2003Go Back to a Safe PlaceGo Back to a Safe Place• Solution:– Provide a way to go back to a checkpoint of the userschoice.• Examples:– The "Home" button on a Web browser– Turning back to the beginning of a chapter in aphysical book or magazine– The “undo" feature on some computer applications
  31. 31. Design Patterns – Eran Toch31November 2003AgendaAgenda• What is a Design Pattern?• Structural Patterns• Behavioral Patterns• Creational Patterns• Human Interaction Patterns• Summary
  32. 32. Design Patterns – Eran Toch32November 2003SummarySummary• Patterns learned:– Composite, Strategy, Singleton, Go Back to a SafePlace.• Advantages of Design Patterns:– They capture expertise and make it accessible to non-experts.– Their names collectively form a vocabulary that helpsdevelopers communicate better.– They help people understand a system more quicklywhen it is documented with the patterns it uses.
  33. 33. Design Patterns – Eran Toch33November 2003ReferencesReferences• Design Patterns: Elements of Reusable Object-OrientedSoftware, Gamma E. et el., 1995, Addison-Wesley.• A course from Bob Tarr from UMBC Universityhttp://www.research.umbc.edu/~tarr/dp/fall00/cs491.html• The Design Patterns Java Companion, James W. Cooper (anonline version of the book)http://www.patterndepot.com/put/8/JavaPatterns.htm• A Site dedicated to Design Patterns by Vince Hustonhttp://home.earthlink.net/~huston2/dp/patterns.html• Seven Habits of Successful Pattern Writers, John Vlissideshttp://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
  34. 34. Design Patterns – Eran Toch34November 2003Doesn’t it go too far?Doesn’t it go too far?• Taken from Vince Huston’s site about DesignPatterns:
  35. 35. Design Patterns – Eran TochMethodologies in Information System DevelopmentThanks!Thanks!