370 likes | 385 Views
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.
E N D
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 • 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 • 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
Abstract Factory Client AbstractFactory ProductA PA2 PA1 Factory1 Factory2 ProductB PB2 PB1
Abstract Factory • Shopping, interlibrary, …
Builder • Add a new conversion? Converter Reader/User ACS Widget Text Picture
Builder Builder Reader/User ConcreteBuilder Product
Builder • Add a new tool, new course, new equipment, …
Factor Method • For creating objects • Drink, Drug, vegetables, gardening, … Product Creator ConcreteProduct ConcreteCreator
Prototype • Provide an abstract tool for defining items like those in the palette. • Windows creation Prototype Client clone movement Prototype1 Prototype2 clone 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, … Adaptee Client Target implementation Adapter request
Bridge • Inheritance binding (Builder? AbstractFactory?) • Windows (their structure and implementation), Database? Implementor Abstraction IA IB RefinedAbstraction
Composite • Group components Component Leaf Composite
Composite aComposite (is aComponent) aComponent aComponent aComponent aLeaf aComposite aLeaf aComponent aComponent aLeaf aLeaf
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
Facade • Simplifying interface to more general facilities of subsystems. (AbstractFactory, FactorMethod, Bridge) • Current screen status of Windows? Facade
Flyweight • For sharing resource FlyWeight Client ConcreteFlyWeight
Flyweight aFlyWeight aClient aCF aClient aCF
Flyweight Paragraph and Keys of keyboard?
Proxy • Access control and reference Subject Proxy RealSubject 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 aHandler aHandler
Command • Request, command, …? • Dos/Shell command, file I/O, user interface, …? Invoker Command Receiver ConcreteCommand
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 Colleague CC1 CC2 ConcreteMediator
Memento • Record the internal state (checkpoints) • Tests of course, firewall, …? Originator Memento Caretaker
Observer • Maintain consistency • Observer calls Getstatus( ) • Ordering, TCP communication, … Subject Observer ConcreteSubject 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 ConcreteAcceptA ConcreteAcceptB ConcreteVisitorA ConcreteVisitorB ConcreteAcceptA ConcreteAcceptB