7 Patterns-442-12-by6

G difgicult to implement with a single parent 38

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: tern Glyph Insert(Glyph, int) Pattern children Composition Traverse() Repair() compositor Compositor Compose () Set_Composition () compositor->Compose() ArrayCompositor 43 LatexCompositor Compose() Simple Compose() 44 Compose() Pattern Pattern Strategy pattern Name Strategy (aka Policy) Applicability Many different algs must be coded for a given problem (formatting, routing, sorting, graph traversal, etc.) Many different classes with similar protocols differ in behavior Need to encapsulate algorithmic information 45 Strategy pattern: Structure Context ContextInterface() strategy Strategy AlgorithmInterface() ConcreteStrategyA AlgorithmInterface() ConcreteStrategyB AlgorithmInterface() ConcreteStrategyC AlgorithmInterface() 46 Pattern Pattern Strategy pattern (cont d) Strategy pattern (cont d) Consequences 1. Clear separation of algorithms from users of those algorithms (clients) e.g., glyphs and formatting algorithms can be added and removed independently of each other Alternative is unappealing (subclassing composition and context) because of proliferation of classes and inability to change algorithms dynamically 2. Elimination of conditional statements (as usual with OO programming) Consequences (continued) 3. Clients must be aware of different strategies (but what about default initializations?) 4. Proliferation of instances at run-time e.g., each glyph would have its instance of strategy with formatting information Again, use a residual object as a default (e.g., see Mlyweight pattern) 48 47 Pattern Pattern Strategy pattern (cont d) Strategy pattern (cont d) Consequences (continued) 5. Interfaces between Context and Strategy Context can pass itself to Strategy, but beware of excessive coupling between classes Alternatively, Strategy can keep reference to Context (again beware of coupling) Alternatively, Context can pass parameters to Strategy before each call 49 Sample code: Based on InterViews system class Composition { public: Composition (Compositor*); void Repair(); private: Composit...
View Full Document

Ask a homework question - tutors are online