7 Patterns-442-12-by6

4 proliferation of instances at run time

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: ow* w) { component->Draw(w);} { MonoGlyph::Draw(w); DrawBorder(w); } 57 G g Pattern Decorator pattern Name Decorator (aka Wrapper) Applicability Add responsibilities to objects dynamically and transparently Handle responsibilities that can be withdrawn at run-time Component Operation() Pattern component Structure of Decorator pattern component-> Operation() ConcreteComp Operation() Decorator Operation() Concrete_Decorator_A Operation() AddedState Concrete_Decorator_B Operation() Added_Op() 59 { Decorator::Operation(); Added_Op(); } 60 Analysis of Decorator pattern Fewer classes than with inheritance Pattern Case study Multiple look-and-feel standards Change Lexi s look-and-feel at run-time Obvious solution has clear disadvantages Use distinct class for each widget and standard Let clients handle different instances for each standard Problems: Proliferation of classes Can t change standard at run-time Code for look-and-feel standard visible to clients, e.g., Button* pb = new MotifButton ; // bad Button* pb = guiFactory->createButton();// good 62 Dynamic addition/removal of decoration supported Keeps root class protocol simple Proliferation of run-time instances Abstract Decorator class needed to provide common interface when multiple decorators used Useful when components are lightweight; otherwise use Strategy Tradeoffs: with Strategy different strategies can have their own interface, hidden from clients (with Decorator added functionality is visible to clients) 61 Case study Pattern Multiple look-and-feel standards Solution: 1. DeGine abstract class GUIFactory with creation methods (deferred) for widgets 2. Concrete subclasses of GUIFactory Actually deGine creation methods for each look-and-feel standard e.g., MotifFactory, MacFactory, etc. 3. Must still specialize each widget into subclasses for each look- and-feel standard 63 Class diagram of GUIFactory GUIfactory CreateMenu() CreateButton() ... MotifFactory CreateMenu() CreateButton() MacFactory CreateMenu() CreateButton() return new MotifMenu;...
View Full Document

Ask a homework question - tutors are online