Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Design Patterns (I) Shyh-Kang Jeng Department of Electrical Engineering National Taiwan University
Reference <ul><li>E. Gamma, R. Helm, R. Johnson, and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented S...
Concept of Design Patterns <ul><li>Each pattern describes a problem which occurs over and over again in our environments, ...
Elements of a Pattern <ul><li>Pattern Name </li></ul><ul><li>Problem </li></ul><ul><li>Solution </li></ul><ul><li>Conseque...
Design Pattern Space
A Case Study: Designing a Document Editor
Document Structure
Graphic Structure
Graphic Structure
Composite Pattern <ul><li>Compose objects into tree structures to represent part-whole hierarchies </li></ul><ul><li>Recur...
Composite Pattern
Class Graphic <ul><li>class Graphic { </li></ul><ul><li>public: </li></ul><ul><li>virtual ~Graphic( ); </li></ul><ul><li>v...
Class Picture <ul><li>class Picture  </li></ul><ul><li>: public Graphic { </li></ul><ul><li>public: </li></ul><ul><li>virt...
Class Rectangle <ul><li>class Rectangle  </li></ul><ul><li>: public Graphic { </li></ul><ul><li>public: </li></ul><ul><li>...
Method  Draw( ) <ul><li>void Picture::Draw( ) </li></ul><ul><li>{ </li></ul><ul><li>Iterator<Graphic*>* i =  </li></ul><ul...
Document Formatting
Strategy Pattern <ul><li>Define a family of algorithms </li></ul><ul><li>Encapsulate each algorithm </li></ul><ul><li>Make...
Strategy Pattern
Strategy Pattern <ul><li>A context may pass all data required by the algorithm to the strategy when the algorithm is calle...
Class Composition <ul><li>class Composition { </li></ul><ul><li>public: </li></ul><ul><li>Composition(Compositor*); </li><...
Class Compositor <ul><li>class Compositor { </li></ul><ul><li>public: </li></ul><ul><li>virtual int Compose( </li></ul><ul...
Method  Repair( ) <ul><li>void Composition::Repair( ) { </li></ul><ul><li>Coord* natural; </li></ul><ul><li>Coord* stretch...
Class SimpleCompositor <ul><li>class SimpleCompositor  </li></ul><ul><li>: public Compositor { </li></ul><ul><li>public: <...
Class ArrayCompositor <ul><li>class ArrayCompositor  </li></ul><ul><li>: public Compositor { </li></ul><ul><li>public: </l...
Invoking Line-Breaking <ul><li>… construct document structure doc </li></ul><ul><li>Composition* quick = new Composition( ...
Transparent Enclosure
Decorator
Decorator Pattern <ul><li>Attach additional responsibilities to an object dynamically </li></ul><ul><li>Provide a flexible...
Decorator Pattern
Class VisualComponent <ul><li>class VisualComponent { </li></ul><ul><li>public: </li></ul><ul><li>VisualComponent( ); </li...
Class Decorator <ul><li>class Decorator :  </li></ul><ul><li>public VisualComponent { </li></ul><ul><li>public: </li></ul>...
Class BorderDecorator <ul><li>class BorderDecorator  </li></ul><ul><li>: public Decorator { </li></ul><ul><li>public: </li...
Invoking Decorators <ul><li>void Window::SetContents(  </li></ul><ul><li>VisualComponet* contents ); </li></ul><ul><li>…… ...
Supporting Multiple GUI’s
Supporting Multiple GUI’s
Abstract Factory Pattern <ul><li>Provide an interface for creating families of related or dependent objects without specif...
Abstract Factory Pattern
Setting GUIFactory <ul><li>GUIFactory* guiFactory; </li></ul><ul><li>…  get style name </li></ul><ul><li>if( styleName == ...
Invoking  ScrollBar( ) <ul><li>ScrollBar* MotifFactory::CreateScrollBar() { </li></ul><ul><li>return new MotifScrollBar; <...
Supporting Multiple Window Systems
Bridge Pattern <ul><li>Decouple an abstraction from its implementation so that the two can vary independently </li></ul><u...
Bridge Pattern
Class Window <ul><li>class Window { </li></ul><ul><li>public: </li></ul><ul><li>Window(View* contents); </li></ul><ul><li>...
Class WindowImp <ul><li>class WindowImp { </li></ul><ul><li>public: </li></ul><ul><li>virtual void DeviceRect( Coord,  Coo...
Class IconWindow <ul><li>class IconWindow : public Window { </li></ul><ul><li>public: </li></ul><ul><li>…… </li></ul><ul><...
Method  DrawContents( ) <ul><li>void IconWindow::DrawContents( ) </li></ul><ul><li>{ </li></ul><ul><li>WindowImp* imp = Ge...
User Operations
Command Pattern <ul><li>Encapsulate a request as an object </li></ul><ul><li>Decouple the object that invokes the operatio...
Command Pattern
Class Command <ul><li>class Command { </li></ul><ul><li>public: </li></ul><ul><li>virtual ~Command( ); </li></ul><ul><li>v...
Class PasteCommand <ul><li>class PasteCommand : public Command { </li></ul><ul><li>public: </li></ul><ul><li>PasteCommand(...
Invoking PasteCommand <ul><li>…  Construct Document structure doc </li></ul><ul><li>PasteCommand* p = new PasteCommand( do...
Spelling Checking and Hyphenation <ul><li>Constraints </li></ul><ul><ul><li>Support multiple algorithms </li></ul></ul><ul...
Iterator Concept
Iterator Pattern <ul><li>Provide a way to access the elements of an aggregate object sequentially without exposing its und...
Iterator Pattern
Class List <ul><li>templare <class Item> </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>List(l...
Class Iterator <ul><li>template <class Item> </li></ul><ul><li>class Iterator { </li></ul><ul><li>public: </li></ul><ul><l...
Class ListIterator <ul><li>template <class Item> </li></ul><ul><li>class ListIterator : public Iterator<Item> { </li></ul>...
Invoking Iterators <ul><li>void Picture::Draw( ) </li></ul><ul><li>{ </li></ul><ul><li>Iterator<Graphic*>* i =  </li></ul>...
cf. Example of STL Iterators <ul><li>Void dump( list< string >& l ){  </li></ul><ul><li>list<const  string>::const_iterato...
Document Structure
Function  SpellingCheck( ) <ul><li>void SpellingCheck( Glyph* g ) { </li></ul><ul><li>Character* c;  </li></ul><ul><li>Row...
Class SpellingChecker <ul><li>class SpellingChecker  </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>Spell...
Method CheckMe( ) <ul><li>void Row::CheckMe(  </li></ul><ul><li>SpellingChecker& checker )  </li></ul><ul><li>{ </li></ul>...
Invoking Spelling Check <ul><li>SpellingChecker spellingChecker; </li></ul><ul><li>Composition* c; </li></ul><ul><li>Glyph...
Visitor Pattern <ul><li>Represent an operation to be performed on the elements of an object structure </li></ul><ul><li>Le...
Visitor Pattern
Visitor Pattern
Class Visitor <ul><li>class Visitor </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>virtual void  VisitCha...
Class SpellingCheckingVisitor <ul><li>class SpellingCheckingVisitor  </li></ul><ul><li>: public Visitor </li></ul><ul><li>...
Class HyphenationVisitor <ul><li>class HyphenationVisitor  </li></ul><ul><li>: public Visitor </li></ul><ul><li>{ </li></u...
Method Accept( ) <ul><li>void Row::Accept(Visitor& visitor)  </li></ul><ul><li>{ </li></ul><ul><li>visitor.VisitRow( this ...
Invoking Spelling Check <ul><li>SpellingCheckingVisitor visitor; </li></ul><ul><li>… construct document structure doc </li...
Invoking Hyphenation <ul><li>HyphenationVisitor visitor;  </li></ul><ul><li>… construct document structure doc </li></ul><...
Upcoming SlideShare
Loading in …5
×

第九章講義(PowerPoint2000 ppt檔, 2661KB)

714 views

Published on

  • Be the first to comment

  • Be the first to like this

第九章講義(PowerPoint2000 ppt檔, 2661KB)

  1. 1. Design Patterns (I) Shyh-Kang Jeng Department of Electrical Engineering National Taiwan University
  2. 2. Reference <ul><li>E. Gamma, R. Helm, R. Johnson, and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley, 1994. </li></ul>
  3. 3. Concept of Design Patterns <ul><li>Each pattern describes a problem which occurs over and over again in our environments, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. </li></ul><ul><li>-- Christopher Alexander, </li></ul><ul><li> A Pattern Language , 1977 </li></ul>
  4. 4. Elements of a Pattern <ul><li>Pattern Name </li></ul><ul><li>Problem </li></ul><ul><li>Solution </li></ul><ul><li>Consequence </li></ul>Observer BarIcon Handler CallQueue Subject
  5. 5. Design Pattern Space
  6. 6. A Case Study: Designing a Document Editor
  7. 7. Document Structure
  8. 8. Graphic Structure
  9. 9. Graphic Structure
  10. 10. Composite Pattern <ul><li>Compose objects into tree structures to represent part-whole hierarchies </li></ul><ul><li>Recursive composition </li></ul><ul><li>Let clients treat individual objects and compositions of objects uniformly </li></ul><ul><li>Easy to add new kinds of components. Clients don’t have to be changed for new component classes </li></ul>
  11. 11. Composite Pattern
  12. 12. Class Graphic <ul><li>class Graphic { </li></ul><ul><li>public: </li></ul><ul><li>virtual ~Graphic( ); </li></ul><ul><li>virtual void Draw(); </li></ul><ul><li>virtual void Add(Graphic*); </li></ul><ul><li>virtual void Remove(Graphic*); </li></ul><ul><li>virtual Graphic* GetChild(int); </li></ul><ul><li>}; </li></ul>
  13. 13. Class Picture <ul><li>class Picture </li></ul><ul><li>: public Graphic { </li></ul><ul><li>public: </li></ul><ul><li>virtual ~Picture( ); </li></ul><ul><li>virtual void Draw( ); </li></ul><ul><li>virtual void Add(Graphic*); </li></ul><ul><li>virtual void Remove(Graphic*); </li></ul><ul><li>virtual Graphic* GetChild(int); </li></ul><ul><li>private: </li></ul><ul><li>list<Graphic*> components; </li></ul><ul><li>}; </li></ul>
  14. 14. Class Rectangle <ul><li>class Rectangle </li></ul><ul><li>: public Graphic { </li></ul><ul><li>public: </li></ul><ul><li>virtual ~Picture( ); </li></ul><ul><li>virtual void Draw( ); </li></ul><ul><li>}; </li></ul>
  15. 15. Method Draw( ) <ul><li>void Picture::Draw( ) </li></ul><ul><li>{ </li></ul><ul><li>Iterator<Graphic*>* i = </li></ul><ul><li>CreateIterator( ); </li></ul><ul><li>for( i->First( ); !i->IsDone( ); </li></ul><ul><li> i->Next( ) ) </li></ul><ul><li> i->CurrentItem( )->Draw( ); </li></ul><ul><li>delete i; </li></ul><ul><li>} </li></ul>
  16. 16. Document Formatting
  17. 17. Strategy Pattern <ul><li>Define a family of algorithms </li></ul><ul><li>Encapsulate each algorithm </li></ul><ul><li>Make the algorithms interchangeable </li></ul><ul><li>Let the algorithm vary independently from clients that use it </li></ul>
  18. 18. Strategy Pattern
  19. 19. Strategy Pattern <ul><li>A context may pass all data required by the algorithm to the strategy when the algorithm is called </li></ul><ul><li>The context can also pass itself as an argument to Strategy operations. That lets the strategy call back on the context as required </li></ul><ul><li>A context forwards requests from its clients to its strategy </li></ul>
  20. 20. Class Composition <ul><li>class Composition { </li></ul><ul><li>public: </li></ul><ul><li>Composition(Compositor*); </li></ul><ul><li>void setComponents( Glyph* ); </li></ul><ul><li>void Repair( ); </li></ul><ul><li>private: </li></ul><ul><li>Compositor* compositor; </li></ul><ul><li>Glyph* components; </li></ul><ul><li>…… </li></ul><ul><li>}; </li></ul>
  21. 21. Class Compositor <ul><li>class Compositor { </li></ul><ul><li>public: </li></ul><ul><li>virtual int Compose( </li></ul><ul><li>Coord natural[], </li></ul><ul><li>Coord stretch[], Coord shrink[], </li></ul><ul><li>int componentCount, </li></ul><ul><li>int lineWidth, </li></ul><ul><li>int breaks[] ) = 0; </li></ul><ul><li>protected: </li></ul><ul><li>Compositor( ); </li></ul><ul><li>}; </li></ul>
  22. 22. Method Repair( ) <ul><li>void Composition::Repair( ) { </li></ul><ul><li>Coord* natural; </li></ul><ul><li>Coord* stretchability; </li></ul><ul><li>Coord* shrinkability; </li></ul><ul><li>int* breaks; </li></ul><ul><li>…… </li></ul><ul><li>int breakCount; </li></ul><ul><li>breakCount = compositor->( </li></ul><ul><li>natural, stretchability, shrinkability, componentCount, lineWidth, breaks ); </li></ul><ul><li>…… </li></ul><ul><li>} </li></ul>
  23. 23. Class SimpleCompositor <ul><li>class SimpleCompositor </li></ul><ul><li>: public Compositor { </li></ul><ul><li>public: </li></ul><ul><li>SimpleCompositor( ); </li></ul><ul><li>virtual int Compose( </li></ul><ul><li>Coord natural[], </li></ul><ul><li>Coord stretch[], Coord shrink[], </li></ul><ul><li>int componentCount, </li></ul><ul><li>int lineWidth, </li></ul><ul><li>int breaks[]); </li></ul><ul><li>…… </li></ul><ul><li>}; </li></ul>
  24. 24. Class ArrayCompositor <ul><li>class ArrayCompositor </li></ul><ul><li>: public Compositor { </li></ul><ul><li>public: </li></ul><ul><li>ArrayCompositor( int interval ); </li></ul><ul><li>virtual int Compose( </li></ul><ul><li>Coord natural[], </li></ul><ul><li>Coord stretch[], Coord shrink[], </li></ul><ul><li>int componentCount, </li></ul><ul><li>int lineWidth, </li></ul><ul><li>int breaks[]); </li></ul><ul><li>…… </li></ul><ul><li>}; </li></ul>
  25. 25. Invoking Line-Breaking <ul><li>… construct document structure doc </li></ul><ul><li>Composition* quick = new Composition( new SimpleCompositor ); </li></ul><ul><li>quick->SetComponents( doc ); </li></ul><ul><li>quick->Repair( ); </li></ul><ul><li>Composition* iconic = new Composition( new ArrayComsopitor(100) ); </li></ul><ul><li>iconic->SetComponents( doc ); </li></ul><ul><li>iconic->Repair( ); </li></ul>
  26. 26. Transparent Enclosure
  27. 27. Decorator
  28. 28. Decorator Pattern <ul><li>Attach additional responsibilities to an object dynamically </li></ul><ul><li>Provide a flexible alternative to subclassing for extending functionality </li></ul><ul><li>Forward requests to its component object </li></ul><ul><li>It may optionally perform additional operations before and after forwarding the request </li></ul>
  29. 29. Decorator Pattern
  30. 30. Class VisualComponent <ul><li>class VisualComponent { </li></ul><ul><li>public: </li></ul><ul><li>VisualComponent( ); </li></ul><ul><li>virtual void Draw( ); </li></ul><ul><li>virtual void Resize( ); </li></ul><ul><li>…… </li></ul><ul><li>}; </li></ul>
  31. 31. Class Decorator <ul><li>class Decorator : </li></ul><ul><li>public VisualComponent { </li></ul><ul><li>public: </li></ul><ul><li>Decorator(VisualComponent*); </li></ul><ul><li>virtual void Draw( ); </li></ul><ul><li>virtual void Resize( ); </li></ul><ul><li>…… </li></ul><ul><li>private: </li></ul><ul><li>VisualComponent* components; </li></ul><ul><li>}; </li></ul>
  32. 32. Class BorderDecorator <ul><li>class BorderDecorator </li></ul><ul><li>: public Decorator { </li></ul><ul><li>public: </li></ul><ul><li>BorderDecorator(VisualComponent*, </li></ul><ul><li>int borderWidth); </li></ul><ul><li>virtual void Draw( ); </li></ul><ul><li>private: </li></ul><ul><li>void DrawBorder(int); </li></ul><ul><li>int width; </li></ul><ul><li>}; </li></ul>
  33. 33. Invoking Decorators <ul><li>void Window::SetContents( </li></ul><ul><li>VisualComponet* contents ); </li></ul><ul><li>…… </li></ul><ul><li>Window* window = new Window; </li></ul><ul><li>TextView* textView = new TextView; </li></ul><ul><li>window->SetContents( </li></ul><ul><li> new BorderDecorator( </li></ul><ul><li> new ScrollDecorator(textView), 1) ); </li></ul>
  34. 34. Supporting Multiple GUI’s
  35. 35. Supporting Multiple GUI’s
  36. 36. Abstract Factory Pattern <ul><li>Provide an interface for creating families of related or dependent objects without specifying their concrete classes </li></ul><ul><li>Defer creation of product objects to its concrete factory subclass </li></ul><ul><li>Create concrete factory instance at run time </li></ul><ul><li>Make exchanging product families easy </li></ul><ul><li>Promote consistency among products </li></ul>
  37. 37. Abstract Factory Pattern
  38. 38. Setting GUIFactory <ul><li>GUIFactory* guiFactory; </li></ul><ul><li>… get style name </li></ul><ul><li>if( styleName == “Motif” ) </li></ul><ul><li>guiFactory = new MotifFactory; </li></ul><ul><li>else if( styleName == “Presentation_Manager” ) </li></ul><ul><li>guiFactory = new PMFactory; </li></ul><ul><li>else if( styleName ==“Macintosh” ) </li></ul><ul><li>guiFactory = new MacFactory; </li></ul><ul><li>else </li></ul><ul><li>guiFactory = new DefaultGUIFactory; </li></ul>
  39. 39. Invoking ScrollBar( ) <ul><li>ScrollBar* MotifFactory::CreateScrollBar() { </li></ul><ul><li>return new MotifScrollBar; </li></ul><ul><li>} </li></ul><ul><li>…… </li></ul><ul><li>ScrollBar* sb = </li></ul><ul><li>guiFactory->CreateScrollBar(); </li></ul>
  40. 40. Supporting Multiple Window Systems
  41. 41. Bridge Pattern <ul><li>Decouple an abstraction from its implementation so that the two can vary independently </li></ul><ul><li>Improve extensibility </li></ul>
  42. 42. Bridge Pattern
  43. 43. Class Window <ul><li>class Window { </li></ul><ul><li>public: </li></ul><ul><li>Window(View* contents); </li></ul><ul><li>virtual void DrawContents( ); </li></ul><ul><li> …… </li></ul><ul><li>protected: </li></ul><ul><li>WindowImp* GetWindowImp( ); </li></ul><ul><li>private: </li></ul><ul><li>WindowImp* m_imp; </li></ul><ul><li>View* m_contents; </li></ul><ul><li>}; </li></ul>
  44. 44. Class WindowImp <ul><li>class WindowImp { </li></ul><ul><li>public: </li></ul><ul><li>virtual void DeviceRect( Coord, Coord, Coord, Coord ) = 0; </li></ul><ul><li>virtual void DeviceText( string, Coord, Coord ) = 0; </li></ul><ul><li>virtual void DeviceBitmap( string, Coord, Coord ) = 0; </li></ul><ul><li> …… </li></ul><ul><li>protected: </li></ul><ul><li>WindowImp( ); </li></ul><ul><li>}; </li></ul>
  45. 45. Class IconWindow <ul><li>class IconWindow : public Window { </li></ul><ul><li>public: </li></ul><ul><li>…… </li></ul><ul><li>virtual void DrawContents( ); </li></ul><ul><li>private: </li></ul><ul><li>string m_bitmapName; </li></ul><ul><li>}; </li></ul>
  46. 46. Method DrawContents( ) <ul><li>void IconWindow::DrawContents( ) </li></ul><ul><li>{ </li></ul><ul><li>WindowImp* imp = GetWindowImp( ); </li></ul><ul><li>if( imp != NULL ) </li></ul><ul><li> imp->DeviceBitmap( m_bitmapName, </li></ul><ul><li>0.0, 0.0 ); </li></ul><ul><li>} </li></ul>
  47. 47. User Operations
  48. 48. Command Pattern <ul><li>Encapsulate a request as an object </li></ul><ul><li>Decouple the object that invokes the operation from the one that knows how to perform it </li></ul><ul><li>Can assemble commands into a composite command (MacroCommand) </li></ul><ul><li>Easy to add new commands, without changing existing classes </li></ul>
  49. 49. Command Pattern
  50. 50. Class Command <ul><li>class Command { </li></ul><ul><li>public: </li></ul><ul><li>virtual ~Command( ); </li></ul><ul><li>virtual void Execute( ) = 0; </li></ul><ul><li>protected: </li></ul><ul><li>Command( ); </li></ul><ul><li>}; </li></ul>
  51. 51. Class PasteCommand <ul><li>class PasteCommand : public Command { </li></ul><ul><li>public: </li></ul><ul><li>PasteCommand(Document* doc); </li></ul><ul><li>virtual void Execute( ); </li></ul><ul><li>private: </li></ul><ul><li>document; </li></ul><ul><li>}; </li></ul><ul><li>void PasteCommand::Execute( ) { </li></ul><ul><li>document->Paste( ); </li></ul><ul><li>} </li></ul>
  52. 52. Invoking PasteCommand <ul><li>… Construct Document structure doc </li></ul><ul><li>PasteCommand* p = new PasteCommand( doc ); </li></ul><ul><li>P->Execute( ); </li></ul>
  53. 53. Spelling Checking and Hyphenation <ul><li>Constraints </li></ul><ul><ul><li>Support multiple algorithms </li></ul></ul><ul><ul><li>Avoid writing functionality into document structure </li></ul></ul><ul><li>Major Works </li></ul><ul><ul><li>Accessing scattered information to be analyzed </li></ul></ul><ul><ul><li>Do the analysis </li></ul></ul>
  54. 54. Iterator Concept
  55. 55. Iterator Pattern <ul><li>Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation </li></ul><ul><li>A concrete iterator keeps track of the current object in the aggregate and can compute the succeeding object in the traversal </li></ul><ul><li>Support variations in the traversal of an aggregate </li></ul><ul><li>Simplify the aggregate interface </li></ul><ul><li>More than one traversal can be in progress at once </li></ul>
  56. 56. Iterator Pattern
  57. 57. Class List <ul><li>templare <class Item> </li></ul><ul><li>class List { </li></ul><ul><li>public: </li></ul><ul><li>List(long size = DEFAULT_LIST_CAPACITY); </li></ul><ul><li>long Count( ) const; </li></ul><ul><li>Item& Get(long index) const; </li></ul><ul><li>ListIterator<Item>* </li></ul><ul><li>CreateIterator( ) const; </li></ul><ul><li>…… </li></ul><ul><li>}; </li></ul>
  58. 58. Class Iterator <ul><li>template <class Item> </li></ul><ul><li>class Iterator { </li></ul><ul><li>public: </li></ul><ul><li>virtual void First( ) = 0; </li></ul><ul><li>virtual void Next( ) = 0; </li></ul><ul><li>virtual bool IsDone( ) = 0; </li></ul><ul><li>virtual Item CurrentItem( ) const = 0; </li></ul><ul><li>protected: </li></ul><ul><li>Iterator( ); </li></ul><ul><li>}; </li></ul>
  59. 59. Class ListIterator <ul><li>template <class Item> </li></ul><ul><li>class ListIterator : public Iterator<Item> { </li></ul><ul><li>public: </li></ul><ul><li>ListIterator(const List<Item>* aList); </li></ul><ul><li>virtual void First( ); </li></ul><ul><li>virtual void Next( ); </li></ul><ul><li>virtual bool IsDone( ) const; </li></ul><ul><li>virtual Item CurrentItem( ) const; </li></ul><ul><li>private: </li></ul><ul><li>const List<Item>* list; </li></ul><ul><li>long current; }; </li></ul>
  60. 60. Invoking Iterators <ul><li>void Picture::Draw( ) </li></ul><ul><li>{ </li></ul><ul><li>Iterator<Graphic*>* i = </li></ul><ul><li>CreateIterator( ); </li></ul><ul><li>for( i->First( ); !i->IsDone( ); </li></ul><ul><li> i->Next( ) ) </li></ul><ul><li> i->CurrentItem( )->Draw( ); </li></ul><ul><li>delete i; </li></ul><ul><li>} </li></ul>
  61. 61. cf. Example of STL Iterators <ul><li>Void dump( list< string >& l ){ </li></ul><ul><li>list<const string>::const_iterator it; </li></ul><ul><li>it = l.begin(); </li></ul><ul><li>While( it != l.end() ) { </li></ul><ul><li>cout << *it << endl; </li></ul><ul><li>it++; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  62. 62. Document Structure
  63. 63. Function SpellingCheck( ) <ul><li>void SpellingCheck( Glyph* g ) { </li></ul><ul><li>Character* c; </li></ul><ul><li>Row* r; </li></ul><ul><li>if( c = dynamic_cast<Character*>(g) ) { </li></ul><ul><li>// analyze the character </li></ul><ul><li>} else if( r = dynamic_cast<Row*>(g) ) { </li></ul><ul><li>// prepare to analyze r’s // children </li></ul><ul><li>} else …… </li></ul><ul><li>} </li></ul>
  64. 64. Class SpellingChecker <ul><li>class SpellingChecker </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>SpellingChecker( ); </li></ul><ul><li>void CheckCharacter( Character* ); </li></ul><ul><li>void CheckRow( Row* ); </li></ul><ul><li>void CheckGraphic( Graphic* ); </li></ul><ul><li>…… </li></ul><ul><li>}; </li></ul>
  65. 65. Method CheckMe( ) <ul><li>void Row::CheckMe( </li></ul><ul><li>SpellingChecker& checker ) </li></ul><ul><li>{ </li></ul><ul><li>checker.CheckRow( this ); </li></ul><ul><li>} </li></ul>
  66. 66. Invoking Spelling Check <ul><li>SpellingChecker spellingChecker; </li></ul><ul><li>Composition* c; </li></ul><ul><li>Glyph* g; </li></ul><ul><li>PreorderIterator i( c ); </li></ul><ul><li>for( i.First( ); !i.IsDone( ); i.Next( ) ) </li></ul><ul><li>{ </li></ul><ul><li>g = i.CurrentItem( ); </li></ul><ul><li>g->CheckMe(spellingChecker); </li></ul><ul><li>} </li></ul>
  67. 67. Visitor Pattern <ul><li>Represent an operation to be performed on the elements of an object structure </li></ul><ul><li>Let you define a new operation without changing the classes of the elements on which it operates </li></ul><ul><li>Gather related operations and separate unrelated ones </li></ul><ul><li>Accumulate state </li></ul>
  68. 68. Visitor Pattern
  69. 69. Visitor Pattern
  70. 70. Class Visitor <ul><li>class Visitor </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>virtual void VisitCharacter( Character* ); </li></ul><ul><li>virtual void VisitRow( Row* ); </li></ul><ul><li>virtual void VisitGraphic( Graphic* ); </li></ul><ul><li>…… </li></ul><ul><li>}; </li></ul>
  71. 71. Class SpellingCheckingVisitor <ul><li>class SpellingCheckingVisitor </li></ul><ul><li>: public Visitor </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>void VisitCharacter( Character* ); </li></ul><ul><li>void VisitRow( Row* ); </li></ul><ul><li>void VisitGraphic( Graphic* ); </li></ul><ul><li>…… </li></ul><ul><li>}; </li></ul>
  72. 72. Class HyphenationVisitor <ul><li>class HyphenationVisitor </li></ul><ul><li>: public Visitor </li></ul><ul><li>{ </li></ul><ul><li>public: </li></ul><ul><li>void VisitCharacter( Character* ); </li></ul><ul><li>void VisitRow( Row* ); </li></ul><ul><li>void VisitGraphic( Graphic* ); </li></ul><ul><li>…… </li></ul><ul><li>}; </li></ul>
  73. 73. Method Accept( ) <ul><li>void Row::Accept(Visitor& visitor) </li></ul><ul><li>{ </li></ul><ul><li>visitor.VisitRow( this ); </li></ul><ul><li>} </li></ul>
  74. 74. Invoking Spelling Check <ul><li>SpellingCheckingVisitor visitor; </li></ul><ul><li>… construct document structure doc </li></ul><ul><li>Glyph* g; </li></ul><ul><li>PreorderIterator i( doc ); </li></ul><ul><li>for( i.First( ); !i.IsDone( ); </li></ul><ul><li>i.Next( ) ) </li></ul><ul><li>{ </li></ul><ul><li>g = i.CurrentItem( ); </li></ul><ul><li>g->Accept(visitor); </li></ul><ul><li>} </li></ul>
  75. 75. Invoking Hyphenation <ul><li>HyphenationVisitor visitor; </li></ul><ul><li>… construct document structure doc </li></ul><ul><li>Glyph* g; </li></ul><ul><li>PreorderIterator i( doc ); </li></ul><ul><li>for( i.First( ); !i.IsDone( ); </li></ul><ul><li>i.Next( ) ) </li></ul><ul><li>{ </li></ul><ul><li>g = i.CurrentItem( ); </li></ul><ul><li>g->Accept(visitor); </li></ul><ul><li>} </li></ul>

×