1 / 26

DESIGN PATTERNS

DESIGN PATTERNS. OZGUR RAHMI DONMEZ. WHAT IS DESIGN PATTERN ?. Design Patterns: Elements of Reusable Object-Oriented Software - GoF (Gang of four) Erich Gamma( JUnit ), Richard Helm, Ralp Johnson,John Vlissides Recurring solutions to common software design problems

flint
Télécharger la présentation

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 OZGUR RAHMI DONMEZ

  2. WHAT IS DESIGN PATTERN ? • Design Patterns: Elements of Reusable Object-Oriented Software -GoF (Gang of four) Erich Gamma(JUnit), Richard Helm, RalpJohnson,JohnVlissides • Recurring solutions to common software design problems • Make OO designs more flexible, elegant and reusable. Improve even documentation and maintenance

  3. WHAT IS DESIGN PATTERN ? • Each pattern describes a problem and which occurs over and over again. • And describes the solution to that problem in such a way that you can use it million times over

  4. WHAT IS DESIGN PATTERN ? • This is not an object oriented course -Encapsulation, Inheritance, polymorphism, interfaces. We assume we know them all. • Do not worry if you don’t understand all patterns at the first sight. -Gof says “We didn’t understand all patterns on the first write” • REFER EXAMPLES AGAIN and AGAIN , FIND PATTERN FITS YOUR PROBLEM

  5. 1.Creational Patterns • Factory • Abstract Factory • Prototype • Builder • Singleton

  6. FACTORY When to use : We have many objects of the same type. And want to delegate creation of this objects from clients to the “object factory”. i.e. : Circle,Square,Line (type : shape) • Decouples client from the instantiated classes -Client code does not change when creating newly added type of product • Reduces code duplication (creation of objects) • Abstract class or interface can be used for abstraction • Example : AsinusLoggerFactory,MCSConnectionFactory…

  7. ABSTRACT FACTORY When to use : We have many objects of the same type and every object belongs to a family. And we want to delegate creation of these objects from clients to the “factory of family factories”. i.e. : MACCircle,MACSquare,MACLine, WindowsCircle,WindowsSquare,WindowsLine, UnixCircle,UnixSquare,UnixLine (type : shape, family : os) • One more level of abstraction Same benefits as Factory pattern. Example : URLPathGeneratorFactory . (factory produced object HTMLURLPathGenerator is also a factory)

  8. SINGLETON When to use : Sometimes it is important to have only one instance of a class. i.e. Manager Classes, Factory classes • Instantiates itself, only one instance • Provides a global point of access • For centralized management of resources

  9. PROTOTYPE When to use : Sometimes object cloning is better than object creation • Object creation is expensive or too complex • Decouples client from the instantiated classes In java it is easier because there is a Cloneable Interface.

  10. BUILDER When to use : -To create complex (or composite) objects with parts those must be created in the same order. -There is a step by step creation sequence. i.e. To draw animal picture : first draw head, second body, third legs…. Same construction sequence can be applied when drawing all animals. • Isolate (or encapsulate) construction process of an object from its representation. Finer control over construction process. • Code reusability.

  11. 2.Behavioral Patterns • Command • Chain of Responsibility • Mediator • Observer • State • Strategy • Visitor • Memento

  12. COMMAND When to use : To encapsulate client requests as objects. We can save (and execute now or later), redo or undo them. They store enough information to be executed later.

  13. CHAIN OF RESPONSIBILITY When to use : We can have several objects that can do some job on the same object but we do not want to know which one or ones actually is doing it. • Decouples the sender of the request to its recievers Example : web filters Example : Response Processors

  14. OBSERVER When to use : Imagine there are number of objects who depend on another object’s state. All dependent objects can be notified (when state changes) with using OBSERVER pattern. Example : ConfigurationManager

  15. MEDIATOR When to use : We have lots of classes interacting each other-spagetti relations between them. • Mediator objects encapsulates all interactions. • Dependency minimized. All classes depends on Mediator.

  16. STRATEGY When to use : One class have more than one behavior (or have behavior family) We can encapsulate these behaviors and make them interchangeable. • Use this pattern if you need to use one of the several algorithms dynamically • Behaviors decoupled from client and managed independently • Switch , if statement hater pattern Examples : BasicAuthenticationStrategy,FacebookAuthenticationStrategy,OpenIdAuthenticationStrategy… JDBC,Spring,HibernateDataAccess strategy…

  17. VISITOR When to use : If there may lots of unrelated operations defined on some objects structures. We can separate (or decouple) those operation logics from objects using VISITOR pattern. • Reduces unnecessary interfaces belong to those operation • Add one more operation is very easy

  18. STATE When to use : Some objects changes behavior when their states changes. Those objects appear to have lots of if statements in the methods. If state == …;else if state == …;else if state == … i.e. : Vending machine • Delegates state behaviors to different STATE classes • If , switch statement hater again 

  19. MEMENTO When to use : To record an object internal state without violating encapsulation, reclaim it later without knowlegde of the original object. • Memento can UNDO via restoring STATE • Command pattern can UNDO via compansating actions

  20. 3.Structural Patterns • Adapter • Facade • Bridge • Decorator • Composite • Proxy

  21. ADAPTER PATTERN When to use : Have a class almost does what I need. But wrong access methods. Has Incompatible interface that I need. Build (a Wrapper class) on incompatible class with adapter pattern. Example : In Java primitive int wrapped by Integer -> To make it object. Has toString,hashcode methods… • Example : You can apply “ADAPTER PATTERN” to 3rd party APIs that you do not have control on them. No

  22. Facade PATTERN When to use : If we want a simpler interface in front of interfaces. • Honestly, two patterns could be implemented the same way programmatically -- the difference is in intent. • Façade is also an adapter (also an wrapper) but it is for providing a simpler “new” interface , not to adapt to an existing one. • A facade is designed to organize multiple services behind a single service gateway. An adapter is designed to provide a way to use a known interface to access an unknown one.

  23. BRIDGE PATTERN When to use : Same UML diagram as STRATEGY pattern  Honestly, two patterns could be implemented the same way programmatically -- the difference is in intent. • Strategy pattern abstracts behaviour. • Strategy pattern abstracts the implementation.

  24. Composite PATTERN When to use : To treat the nodes and the leafs in the same manner in the tree structure. Example : Directory is also handled like a file. File and Directory has some common interface. There is a tree structure. • Client doesn’t know the differences between the composition of object(Node) and the individual object(Leaf).

  25. Decorator PATTERN When to use : To add additional responsibilities dynamically without affecting the original object and other objects. • Uses composition instead of inheritance. • More flexibility than inheritance • Object hierarchy does not grow deeper • Example : sitemesh

  26. Proxy PATTERN When to use : To control access to an object. • Representing expensive resource or impossible to duplicate: Network connection, large object in memory, open file WHAT IS THE DIFFERENCE (PROXY and DECORATOR) • Decorator Pattern focuses on dynamically adding functions to an object • Proxy Pattern focuses on controlling access to an object • In Proxy Pattern, we usually create an instance of abject inside the proxy class. • In Decorator Pattern, we typically pass the original object as a parameter to the constructor of the decorator.

More Related