Prototype Pattern

2,430 views

Published on

For Design Pattern Class Presentation

Published in: Education, Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,430
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
69
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Prototype Pattern

  1. 1. Prototype pattern<br />Yu Chen &Ider Zheng<br />CSE 776 - Design Patterns by Jim Fawcett<br />Fall 2011<br />
  2. 2. Who?<br />What? <br />Why? <br />When?<br />How?<br />4W1H<br />Y & I<br />Structure/Participants<br />Consequences<br />Applicability<br />Implementation<br />
  3. 3. Intent<br />“Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.”<br />
  4. 4. Real life analogy<br />Copy Machine<br />
  5. 5. Real life analogy<br />Copy Machine<br />Product Prototypes<br />Die Sinking<br />
  6. 6. structure<br />
  7. 7. structure<br />Prototype<br />Concrete<br />Concrete<br />Client<br />Clone()<br />Clone()<br />Clone()<br />Operation()<br />p = prototype->Clone()<br />Return copy of itself<br />Return copy of itself<br />
  8. 8. participants<br />Prototype<br />Declares an instance for cloning itself<br />Concrete Prototype<br />Implement an operation of cloning itself<br />Client<br />Creates an object by asking the prototype to clone itself<br />
  9. 9. consequences<br />Specifying new objects by varying values.<br />Specifying new objects by varying structure.<br />Reduced subclassing.<br />Configuring an application with classes dynamically.<br />Adding and removing products at run-time.<br />
  10. 10. applicability<br />When a system should be independent of how its products are created, composed, and represented.<br />When the classes to instantiate are specified at run-time<br />To avoid building a class hierarchy of factories that parallels the class hierarchy of products<br />When instances of a class can have one of only a few different combinations of state.<br />
  11. 11. Implementation issues<br />Implementing the Clone operation.<br />Binding the concrete prototypes<br />
  12. 12. Copy Constructor <br />VS<br />Prototype Pattern<br />
  13. 13. Shallow Copy<br />VS<br />Deep Copy<br />
  14. 14. Implementation issues<br />Implementing the Clone operation.<br />Binding the concrete prototypes<br />
  15. 15. structure<br />?<br />Prototype<br />Concrete<br />Concrete<br />Client<br />Clone()<br />Clone()<br />Clone()<br />Operation()<br />p = prototype->Clone()<br />Return copy of itself<br />Return copy of itself<br />
  16. 16. Factories<br />Factory: <br />private ConcreteA* _prototypeA; // we can have more<br />Factory: <br />public MakeA() { return _prototypeA->clone(); }<br />Client: <br />Base* obj = MakeA(); <br />Client: <br />obj->MoreActions();<br />
  17. 17. Prototype managers<br />Create and destroy prototype at run-time<br />Register new prototypes<br />Unregister prototypes<br />Client retrieve and store prototype from prototype manager<br />Use map<key,prototype><br />
  18. 18. CASE STUDY - tetris<br />
  19. 19. Tetris cont.<br />
  20. 20. Tetris cont.<br />
  21. 21. Tetris cont.<br />
  22. 22. Tetris cont.<br />
  23. 23. Tetris Cont.<br />
  24. 24. Tetris cont.<br />
  25. 25. Tetris cont.<br />
  26. 26. Tetris cont.<br />
  27. 27. One more thing<br />Thank You!<br />

×