资源预览内容
第1页 / 共91页
第2页 / 共91页
第3页 / 共91页
第4页 / 共91页
第5页 / 共91页
第6页 / 共91页
第7页 / 共91页
第8页 / 共91页
第9页 / 共91页
第10页 / 共91页
亲,该文档总共91页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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+ Standard 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 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:vector 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 structuralvoid 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 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 wherever 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: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);compositor.compose (); private:/ Data structures for composition. ; Composition comp; TexCompositor tc; comp.perform_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) STRATEGY 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.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 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 algorithm 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 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, 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号