1 / 24

Applying The “gang of four” (GoF) design Patterns

Applying The “gang of four” (GoF) design Patterns. Purpose. Creational. Structural. Behavioral. Scope. Class. Factory method. Adapter (class). Interpreter Template method. Object. Abstract factory Builder Prototype Singleton. Adapter (object) Bridge Composite Decorator Façade

will
Télécharger la présentation

Applying The “gang of four” (GoF) 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. Applying The “gang of four” (GoF) design Patterns

  2. Purpose Creational Structural Behavioral Scope Class Factory method Adapter (class) Interpreter Template method Object Abstract factory Builder Prototype Singleton Adapter (object) Bridge Composite Decorator Façade Flyweight Proxy Chain of responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Design Pattern Space

  3. Creational Patterns • Abstract Factory: • Factory for building related objects • Builder: • Factory for building complex objects incrementally • Factory Method: • Method in a derived class creates associates • Prototype: • Factory for cloning new instances from a prototype • Singleton: • Factory for a singular (sole) instance

  4. Structural Patterns • Adapter: • Translator adapts a server interface for a client • Bridge: • Abstraction for binding one of many implementations • Composite: • Structure for building recursive aggregations • Decorator: • Decorator extends an object transparently • Facade: • Simplifies the interface for a subsystem • Flyweight: • Many fine-grained objects shared efficiently. • Proxy: • One object approximates another

  5. Behavioral Patterns • Chain of Responsibility: • Request delegated to the responsible service provider • Command: • Request is first-class object • Iterator: • Aggregate elements are accessed sequentially • Interpreter: • Language interpreter for a small grammar • Mediator: • Coordinates interactions between its associates • Memento: • Snapshot captures and restores object states privately

  6. Behavioral Patterns (cont.) • Observer: • Dependents update automatically when subject changes • State: • Object whose behavior depends on its state • Strategy: • Abstraction for selecting one of many algorithms • Template Method: • Algorithm with some steps supplied by a derived class • Visitor: • Operations applied to elements of a heterogeneous object structure

  7. Observer Pattern • Intent: • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically • Key forces: • There may be many observers • Each observer may react differently to the same notification • The subject should be as decoupled as possible from the observers to allow observers to change independently of the subject

  8. Structure of Observer Pattern

  9. Collaborations in Observer Pattern

  10. Adapter pattern Problem: How to resolve incompatible interfaces or provide a stable interface to similar components with different interfaces? Solution: Convert original interface component into another one through an intermediate adapter. Use interfaces and polymorphism to add indirection to varying APIs

  11. Structure of Adapter in UML

  12. Using an Adapter: adapt postSale request to SOAP XML interface Fig. 26.2

  13. Adapter Pattern • Consequences: • A class adapter • adapts Adaptee to Target by committing to a concrete Adapter class. • lets Adapter override some of Adaptee's behavior, since Adapter is a subclass of Adaptee. • introduces only one object, and no additional pointer indirection is needed to get to the adaptee. • An object adapter • lets a single Adapter work with many • makes it harder to override Adaptee behavior. • Known uses: • WindowAdapter in AWT/Swing API • JDBC-ODBC API • Can you identify more? • Related patterns: • Bridge, Proxy

  14. Benefits of Adapter pattern • Reduces coupling to implementation specific details • Polymorphism and Indirection reveals essential behavior provided • Including name of design pattern in new class (e.g., TaxMasterAdapter) in class diagrams and code communicates to other developers in terms of known design patterns

  15. Singleton pattern (creational) • A class with just instance and provide a global point of access • Global Variables can be dangerous! (side effects, break information hiding) class Singleton { public: static Singleton* getInstance(); protected: //Why are the following protected? Singleton(); Singleton(const Singleton&); Singleton& operator= (const Singleton&); private: static Singleton* instance; }; Singleton *p2 = p1->getInstance();

  16. Singleton Pattern • Intent: Ensure a class only has one instance, and provide a global point of access to it • Applicability: Use Singleton when • there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point • when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code

  17. Singleton UML

  18. Singleton Pattern • Consequences: • Controlled access to sole instance. • Reduced name space. • Permits refinement of operations and representation. • Permits a variable number of instances. • More flexible than class operations. • Known uses: • LogFactory in Apache Log4j • SecretKeyFactory in crypto API (javax.crypto.SecretKeyFactory ) • Can you identify more? • Related patterns: • Many patterns use Singleton – AbstractFactory, Builder, Prototype

  19. Pattern: Abstract Factory (a variation of Factory Method, Abstract Factory) a class used to create other objects 19

  20. Problem: Bulky GUI code GUI code to construct many components quickly becomes redundant (here, with menus): homestarItem = new JMenuItem("Homestar Runner"); homestarItem.addActionListener(this); viewMenu.add(homestarItem); crapItem = new JMenuItem("Crappy"); crapItem.addActionListener(this); viewMenu.add(crapItem); another example (with buttons): button1 = new JButton(); button1.addActionListener(this); button1.setBorderPainted(false); button2 = new JButton(); button2.addActionListener(this); button2.setBorderPainted(false); 20

  21. Factory pattern Factory: a class whose sole job is to easily create and return instances of other classes a creational pattern; makes it easier to construct complex objects instead of calling a constructor, use a static method in a "factory" class to set up the object saves lines and complexity to quickly construct / initialize objects examples in Java: borders (BorderFactory), key strokes (KeyStroke), network connections (SocketFactory) 21

  22. Using existing factories in Java setting borders on buttons and panels use built-in BorderFactory class myButton.setBorder( BorderFactory.createRaisedBevelBorder()); setting hot-key "accelerators" on menus use built-in KeyStroke class menuItem.setAccelerator(KeyStroke.getKeyStroke('T', KeyEvent.ALT_MASK)); 22

  23. Factory implementation details when implementing a factory of your own: the factory itself should not be instantiated make constructor private factory only uses static methods to construct components factory should offer as simple an interface to client code as possible don't demand lots of arguments; possibly overload factory methods to handle special cases that need more arguments factories are often designed for reuse on a later project or for general use throughout your system 23

  24. GoF's variations on Factory Abstract Factory pattern: when the topmost factory class and its creational method are abstract 24

More Related