《设计模式案例(三)》由会员分享,可在线阅读,更多相关《设计模式案例(三)(91页珍藏版)》请在金锄头文库上搜索。
1、An Introduction to Design PatternsDouglas C. Schmidt Vanderbilt Universitywww.dre.vanderbilt.edu/schmidt/gof.ppt schmidtdre.vanderbilt.edu Based on material produced by John Vlissides1OverviewPart I: Motivation i != consumers.end (); i+)(*i).push (event);CORBA Notification Service example using C+ S
2、tandard Template Library (STL) iterators (which is an example of the Iterator pattern from GoF)class MyPushConsumer : public / .virtual void push (const CORBA:Any / .virtual void add_child (Glyph *) protected:int x_, y_; / Coordinate position. ;class Character : public Glyph public:Character (const
3、std:string / .virtual void draw (const Drawing_Region private:std:string name_; ;class Row : public Glyph public:Row (std:vector children);/ .virtual void draw (const Drawing_Region i != children_.end (); i+)(*i)-draw (c);/ .virtual void add_child (Glyph *g) children_.push_back (g); private:std:vect
4、or children_;/ . ;ComponentCompositeLeaf26Document Structure (contd) COMPOSITE object structuralCORBA Naming Service example using CosNaming:BindingIterator (which is an example of the “Batch Iterator” pattern compound from POSA5)Composite NodeLeaf Node27Document Structure (contd) COMPOSITE object s
5、tructuralvoid show_chunk (const CosNaming:BindingList_ptr i resolve (bli.binding_name);list_context (CosNaming:NamingContext:_narrow (obj);else cout list (CHUNK, bl, it); / Get first chunkshow_chunk (bl, nc); / Print first chunkif (!CORBA:is_nil(it) / More bindings?while (it-next_n(CHUNK, bl) / Get
6、next chunkshow_chunk (bl, nc); / Print chunkit-destroy(); / Clean up Handle Composite NodeHandle Leaf Node28Document Structure (contd) COMPOSITE (contd) object structuralConsequences +uniformity: treat components the same regardless of complexity +extensibility: new Component subclasses work whereve
7、r old ones do overhead: might need prohibitive numbers of objects Awkward designs: may need to treat leaves as lobotomized composites Implementationndo Components know their parents?nuniform interface for both leaves Simple algorithmComplex algorithmclass SimpleCompositor : public Compositor public:
8、virtual void compose () /* . */ ;class Composition : public Glyph public:void perform_composition (const Compositor for (std:vector:iterator i (leaf_glyphs); i != leaf_glyphs.end (); i+) this-insert (*i);pose (); private:/ Data structures for composition. ; Composition comp; TexCompositor tc; comp.p
9、erform_composition (tc, leaf_glyphs);SimpleCompositor sc; comp.perform_composition (sc, leaf_glyphs);class Compositor public:void set_context (Composition virtual void compose () = 0;/ . ;Creates row-column structure as directed by compositorStrategy can be changed dynamically!34Formatting (contd) S
10、TRATEGY object behavioralHook for the concurrency strategyHook for the request demuxing strategyHook for marshaling strategyHook for the connection management strategyHook for the underlying transport strategyHook for the event demuxing strategyStrategy can also be applied in distributed systems (e.
11、g., middleware)35Formatting (contd) STRATEGY (contd) object behavioralConsequences +greater flexibility, reuse +can change algorithms dynamically strategy creation for (std:vector:iterator i (leaf_glyphs); i != leaf_glyphs.end (); i+) insert (*i);compose ();virtual void compose () = 0; / Hook Method
12、 protected:/ Data structures for composition. ; class Simple_Composition : public Composition virtual void compose () /* . */ ;class Tex_Composition : public Composition virtual void compose () /* . */ ;37Formatting (contd) Template Method (contd) class behavioralIntentnProvide a skeleton of an algo
13、rithm in a method, deferring some steps to subclasses class Base_Class public:/ Template Method. void template_method (void) hook_method_1 ();hook_method_2 ();/ . protected:virtual void hook_method_1 () = 0; virtual void hook_method_2 () = 0; ; class Derived_Class_1 : public Base_Class virtual void
14、hook_method_2 () /* . */ ;class Derived_Class_2 : public Base_Class virtual void hook_method_1 () /* . */ virtual void hook_method_2 () /* . */ ;38EmbellishmentGoals:nadd a frame around text compositionnadd scrolling capabilityConstraints/forces:nembellishments should be reusable without subclassing
15、, i.e., so they can be added dynamically at runtimenshould go unnoticed by clients39Embellishment (contd) Solution: “Transparent” EnclosureMonoglyphnbase class for glyphs having one childnoperations on MonoGlyph (ultimately) pass through to childMonoGlyph subclasses:nFrame: adds a border of specified widthnScroller: scrolls/clips child, adds scrollbars40Embellishment (contd) MonoGlyph HierarchyClass Mo