Design PatternDesign Pattern
Dr. Zhen JiangWest Chester University
E-mail: [email protected]: www.cs.wcupa.edu/~zjiang
OutlineOutline
IntroductionCreational PatternsStructural PatternsBehavioral Patterns
IntroductionIntroduction 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 PatternCreational Pattern
Abstract factoryBuilderFactory MethodPrototypeSingleton
Abstract FactoryAbstract Factory
ClientWidgetFactory
Motif
Consider a user interface (client, windows and ScrollBar) that supports (creates) multiple look-and-feel standards.
PMW
Windows
PMW Motif
ScrollBar
PMW Motif
?
Abstract FactoryAbstract Factory
ClientAbstractFactory
Factory1 Factory2
ProductA
PA2 PA1
ProductB
PB2 PB1
Abstract FactoryAbstract Factory Shopping, interlibrary, …
BuilderBuilder
Reader/User
Add a new conversion?
Converter
ACS Widget
Text Picture
BuilderBuilder
Reader/User Builder
ConcreteBuilder
Product
BuilderBuilder Add a new tool, new course, new equipment, …
Factor MethodFactor Method For creating objects Drink, Drug, vegetables, gardening, …
CreatorProduct
ConcreteProduct ConcreteCreator
PrototypePrototype Provide an abstract tool for defining items like
those in the palette. Windows creation
PrototypeClient
movement
clone
Prototype1
clone
Prototype2
clone
SingletonSingleton For some classes to have exactly one instance Examples?
Structural PatternStructural Pattern
AdapterBridgeCompositeDecoratorFaçadeFlyweightProxy
AdapterAdapter For existing and unrelated classes working in an
application that expects classes with a different and incompatible interfaces (Builder?)
Unix process, file system, …
TargetClient
Adapter
request
Adaptee
implementation
BridgeBridge Inheritance binding (Builder? AbstractFactory?) Windows (their structure and implementation),
Database?
RefinedAbstraction
Abstraction Implementor
IA IB
CompositeComposite Group components
Component
Leaf Composite
CompositeComposite
aComposite (is aComponent)
aComponent aComponentaComponentaLeaf aLeaf aComposite
aComponentaComponent
aLeaf aLeaf
DecoratorDecorator Specifying the responsibility of certain group,
build an transparent interface to component’s client
Teaching, GA, and Instructor
Component
Leaf Decorator
DA DBops
ops ops
FacadeFacade Simplifying interface to more general facilities of
subsystems. (AbstractFactory, FactorMethod, Bridge)
Current screen status of Windows?
Facade
FlyweightFlyweight For sharing resource
Client
FlyWeight
ConcreteFlyWeight
FlyweightFlyweight
aClient
aClientaFlyWeight
aCF
aCF
FlyweightFlyweight
Paragraph and Keys of keyboard?
ProxyProxy Access control and reference
Subject
RealSubjectProxy
aClient
aProxy
aRS
ProxyProxy Network Service?
Behavioral PatternBehavioral Pattern Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor
Chain of ResponsibilityChain of Responsibility Decouple senders and receivers (sequence? ) Steps, courses, …
Handler
aHandleraHandler
aHandler
CommandCommand Request, command, …? Dos/Shell command, file I/O, user interface, …?
Invoker Command
ConcreteCommandReceiver
InterpreterInterpreter Grammar Composite? Formal language L=a|bC; C=e|cC. (sample
statement: a, b, bc, bcc, …
IteratorIterator Access without exposing any internal structure Interface FactorMethod
MediatorMediator Distribution of behavior among objects AbstractFactory
Mediator
ConcreteMediator
Colleague
CC1 CC2
MementoMemento Record the internal state (checkpoints) Tests of course, firewall, …?
Originator Memento Caretaker
ObserverObserver Maintain consistency Observer calls Getstatus( ) Ordering, TCP communication, …
Subject
ConcreteSubject
Observer
ConcreteObserver
StateState Conditional statements Used in Command, Memento, Observer, … Project, study, …,
Context State
ConcreteStateA
ConcreteStateB
StrategyStrategy Builder, Prototype, Bridge, Command, State. Hollywoodsquare, Chess, searching and sorting
(for example, shell), …
Context Strategy
ConcreteStrategyA
ConcreteStrategyB
Template MethodTemplate 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
VisitorVisitor 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
ConcreteAcceptAConcreteAcceptB
ConcreteAcceptA
ConcreteVisitorB
ConcreteAcceptB