Design Pattern Dr. Zhen Jiang West Chester University url:
Outline Introduction Creational Patterns Structural Patterns Behavioral Patterns
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.
Creational Pattern Abstract factory Builder Factory Method Prototype Singleton
Abstract Factory Client WidgetFactory Motif Consider a user interface (client, windows and ScrollBar) that supports (creates) multiple look- and-feel standards. PMW Windows PMWMotif ScrollBar PMWMotif ?
Abstract Factory Client AbstractFactory Factory1Factory2 ProductA PA2PA1 ProductB PB2PB1
Abstract Factory Shopping, interlibrary, …
Builder Reader/User Add a new conversion? Converter ACSWidget TextPicture
Builder Reader/User Builder ConcreteBuilder Product
Builder Add a new tool, new course, new equipment, …
Factor Method For creating objects Drink, Drug, vegetables, gardening, … Creator Product ConcreteProduct ConcreteCreator
Prototype Provide an abstract tool for defining items like those in the palette. Windows creation Prototype Client movement clone Prototype1 clone Prototype2 clone
Singleton For some classes to have exactly one instance Examples?
Structural Pattern Adapter Bridge Composite Decorator Façade Flyweight Proxy
Adapter For existing and unrelated classes working in an application that expects classes with a different and incompatible interfaces (Builder?) Unix process, file system, … Target Client Adapter request Adaptee implementation
Bridge Inheritance binding (Builder? AbstractFactory?) Windows (their structure and implementation), Database? RefinedAbstraction Abstraction Implementor IAIB
Composite Group components Component LeafComposite
Composite aComposite(is aComponent) aComponent aLeaf aComposite aComponent aLeaf
Decorator Specifying the responsibility of certain group, build an transparent interface to component’s client Teaching, GA, and Instructor Component LeafDecorator DADB ops
Facade Simplifying interface to more general facilities of subsystems. (AbstractFactory, FactorMethod, Bridge) Current screen status of Windows? Facade
Flyweight For sharing resource Client FlyWeight ConcreteFlyWeight
Flyweight aClient aFlyWeight aCF
Flyweight Paragraph and Keys of keyboard?
Proxy Access control and reference Subject RealSubject Proxy aClient aProxy aRS
Proxy Network Service?
Behavioral Pattern Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor
Chain of Responsibility Decouple senders and receivers (sequence? ) Steps, courses, … Handler aHandler
Command Request, command, …? Dos/Shell command, file I/O, user interface, …? InvokerCommand ConcreteCommandReceiver
Interpreter Grammar Composite? Formal language L=a|bC; C=e|cC. (sample statement: a, b, bc, bcc, …
Iterator Access without exposing any internal structure Interface FactorMethod
Mediator Distribution of behavior among objects AbstractFactory Mediator ConcreteMediator Colleague CC1CC2
Memento Record the internal state (checkpoints) Tests of course, firewall, …? OriginatorMemento Caretaker
Observer Maintain consistency Observer calls Getstatus( ) Ordering, TCP communication, … Subject ConcreteSubject Observer ConcreteObserver
State Conditional statements Used in Command, Memento, Observer, … Project, study, …, Context State ConcreteStateA ConcreteStateB
Strategy Builder, Prototype, Bridge, Command, State. Hollywoodsquare, Chess, searching and sorting (for example, shell), … Context Strategy ConcreteStrategyA ConcreteStrategyB
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
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 ConcreteVisitorA ConcreteAcceptA ConcreteAcceptB ConcreteAcceptA ConcreteVisitorB ConcreteAcceptB