Professional Documents
Culture Documents
Aditya P. Mathur
Purdue University
Organization
Patterns: Behavioral: Observer Structural: Faade Creational Abstract Factory Factory Method Singleton
Excellent reference: Design Patterns book by Erich Gamma, et al., Addison-Wesley, 1994.
A B C D X Y Z 15 35 35 15 10 40 30 20 10 40 30 20 A B C D D
A B C
Application data
Observer
Update()
Concrete Subject
GetState() SetState() subjectState
CS 406: Design Patterns
Update() observerState
protected:
observer ();
};
public:
virtual ~Subject;
virtual void Attach (observer*); virtual void Detach (observer*) ; virtual void Notify();
protected:
Subject ();
private:
List <Observer*> *_observers;
};
CS 406: Design Patterns 10
11
12
13
private:
ClockTimer* _subject; }
14
} DigitalClock ::~DigitalClock() {
_subject->Detach(this);
15
int minute = _subject->GeMinute(); // etc. // Code for drawing the digital clock. }
CS 406: Design Patterns 16
17
Subsystem classes
20
Facade
Subsystem classes
CS 406: Design Patterns 21
23
Promotes weak coupling between subsystem and its clients. Helps in layering the system. Helps eliminate circular dependencies.
24
Example: A compiler
Compiler
Compile() Stream Scanner BytecodeStream Parser CodeGenerator PnodeBuilder Pnode Symbol Token Invocations
ExpressionNode
25
Private:
istream& _inputStream;
};
26
};
27
) const;
virtual Pnode* NewAssignment ( Pnode* variable, Pnode* expression
) const; Private:
Pnode* _node;
// Similarly...more nodes.
};
28
// .
virtual void traverse (Codegenerator&);
protected:
PNode();
};
CS 406: Design Patterns 29
// Generate bytecode.
// . Protected:
CodeGenerator (BytecodeStream&); BytecodeStream& _output;
};
30
}; };
31
34
Reuse the main body of an Application and write the code it calls
Defines the architecture Of the application
Toolkit
Framework
Toolkits: Collection of related and reusable classes e.g. C++ I/O stream library Framework: A set of cooperating classes that make up a reusable design for a specific class of applications e.g. drawing, compilers, CAD/CAM etc.
CS 406: Design Patterns 35
36
How to design the application so that incorporating new look and feel requirements will be easy?
37
3.
4.
Client
WWidgetFactory
ScrollBar
MacWidgetFactory
WScrollBar
39
Client
ConcreteFactory1
AbstractProductB
ConcreteFactory2
CreateProductA() CreateProductB()
ProductB2
ProductB1
40
// This factory is a collection of // factory methods. Also, this class // acts both as Abstract and Concrete // Factory
// more methods. }
CS 406: Design Patterns 42
// One can also create a // BombedMazeFactory with // different types of Rooms // and Walls.
43
45
2.
3.
46
docs Document
Open() Close() Save() * 1
Application
CreateDoc() NewDoc() OpenDoc()
MyApplication MyDocument
CreateDoc() Document* doc=CreateDoc(); docs.Add(doc); doc Open();
CS 406: Design Patterns 47
ConcreteCreator ConcreteProduct
FactoryMethod()
Creator (Application): Declares factory method which returns an object of type Product. Also, may define the factory method to create a Product object.
50
Singleton Structure
Singleton
static uniqueInstance singletonData static Instance() SingletonOp() GetSingletonData() return uniqueinstance
51