1 / 37

Design Pattern

Design Pattern. Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: www.cs.wcupa.edu/~zjiang. Outline. Introduction Creational Patterns Structural Patterns Behavioral Patterns. Introduction.

asher
Télécharger la présentation

Design Pattern

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 Pattern Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu url: www.cs.wcupa.edu/~zjiang

  2. Outline • Introduction • Creational Patterns • Structural Patterns • Behavioral Patterns

  3. Introduction • A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design • Creational design patterns abstracts the instantiation process • Structural patterns are concerned with how classes and objects are composed to form larger structures. • Behavioral patterns are concerned with algorithms and assignment of responsibilities between objects.

  4. Creational Pattern • Abstract factory • Builder • Factory Method • Prototype • Singleton

  5. Abstract Factory • Consider a user interface (client, windows and ScrollBar) that supports (creates) multiple look-and-feel standards. Client WidgetFactory Windows PMW Motif Motif PMW ScrollBar ? PMW Motif

  6. Abstract Factory Client AbstractFactory ProductA PA2 PA1 Factory1 Factory2 ProductB PB2 PB1

  7. Abstract Factory • Shopping, interlibrary, …

  8. Builder • Add a new conversion? Converter Reader/User ACS Widget Text Picture

  9. Builder Builder Reader/User ConcreteBuilder Product

  10. Builder • Add a new tool, new course, new equipment, …

  11. Factor Method • For creating objects • Drink, Drug, vegetables, gardening, … Product Creator ConcreteProduct ConcreteCreator

  12. Prototype • Provide an abstract tool for defining items like those in the palette. • Windows creation Prototype Client clone movement Prototype1 Prototype2 clone clone

  13. Singleton • For some classes to have exactly one instance • Examples?

  14. Structural Pattern • Adapter • Bridge • Composite • Decorator • Façade • Flyweight • Proxy

  15. Adapter • For existing and unrelated classes working in an application that expects classes with a different and incompatible interfaces (Builder?) • Unix process, file system, … Adaptee Client Target implementation Adapter request

  16. Bridge • Inheritance binding (Builder? AbstractFactory?) • Windows (their structure and implementation), Database? Implementor Abstraction IA IB RefinedAbstraction

  17. Composite • Group components Component Leaf Composite

  18. Composite aComposite (is aComponent) aComponent aComponent aComponent aLeaf aComposite aLeaf aComponent aComponent aLeaf aLeaf

  19. Decorator • Specifying the responsibility of certain group, build an transparent interface to component’s client • Teaching, GA, and Instructor Component Leaf Decorator ops DA DB ops ops

  20. Facade • Simplifying interface to more general facilities of subsystems. (AbstractFactory, FactorMethod, Bridge) • Current screen status of Windows? Facade

  21. Flyweight • For sharing resource FlyWeight Client ConcreteFlyWeight

  22. Flyweight aFlyWeight aClient aCF aClient aCF

  23. Flyweight Paragraph and Keys of keyboard?

  24. Proxy • Access control and reference Subject Proxy RealSubject aClient aProxy aRS

  25. Proxy • Network Service?

  26. Behavioral Pattern • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor

  27. Chain of Responsibility • Decouple senders and receivers (sequence? ) • Steps, courses, … Handler aHandler aHandler aHandler

  28. Command • Request, command, …? • Dos/Shell command, file I/O, user interface, …? Invoker Command Receiver ConcreteCommand

  29. Interpreter • Grammar • Composite? • Formal language L=a|bC; C=e|cC. (sample statement: a, b, bc, bcc, …

  30. Iterator • Access without exposing any internal structure • Interface • FactorMethod

  31. Mediator • Distribution of behavior among objects • AbstractFactory Mediator Colleague CC1 CC2 ConcreteMediator

  32. Memento • Record the internal state (checkpoints) • Tests of course, firewall, …? Originator Memento Caretaker

  33. Observer • Maintain consistency • Observer calls Getstatus( ) • Ordering, TCP communication, … Subject Observer ConcreteSubject ConcreteObserver

  34. State • Conditional statements • Used in Command, Memento, Observer, … • Project, study, …, Context State ConcreteStateA ConcreteStateB

  35. Strategy • Builder, Prototype, Bridge, Command, State. • Hollywoodsquare, Chess, searching and sorting (for example, shell), … Context Strategy ConcreteStrategyA ConcreteStrategyB

  36. Template Method • A template method defines an algorithm in terms of abstract operations that subclasses override to provide concrete behavior • Inheritance (generalization), virtual function • Operations (searching and sorting) in Template class (Template Stack, Template Queue). AbstractClass ConcreteClass

  37. Visitor • Packaging related operations from each class in a separate object, called a visitor, and passing it to elements of the abstract syntax tree as it’s traversed. Visitor ConcreteAcceptA ConcreteAcceptB ConcreteVisitorA ConcreteVisitorB ConcreteAcceptA ConcreteAcceptB

More Related