03 Composite and Flyweight Pattern<br />Design Patterns<br />© Prafulla Paraskar 2010<br />
Composite<br />Structural Design Pattern<br />© Prafulla Paraskar 2010<br />
Definition<br />The composite pattern describes that a group of objects are to be treated in the same way as a single inst...
Composite Pattern – Explained (1/2)<br />© Prafulla Paraskar 2010<br />
Composite Pattern – Explained (2/2)<br />Arranges structured hierarchies so that single components and groups of component...
Composite Pattern – UML Diagram<br />© Prafulla Paraskar 2010<br />
Composite Pattern – Real World Samples<br />Files and folders in Windows<br />Organization Tree<br />Arithmetic Expression...
Composite Pattern – Guidelines<br />You have :<br /><ul><li>An irregular structure of objects and composites of the object...
To treat all objects in a composite uniformly.</li></ul>© Prafulla Paraskar 2010<br />
Flyweight<br />Structural Design Pattern<br />© Prafulla Paraskar 2010<br />
Definition<br />The Flyweight pattern is a pattern for sharing objects, where each instance does not contain its own state...
Flyweight Pattern – Explained (1/3)<br />Designing objects down<br /> to the lowest levels of<br /> system "granularity”<b...
Flyweight Pattern – Explained (2/3)<br />column<br />row<br />row<br />row<br />a<br />f<br />e<br />d<br />c<br />b<br />...
Flyweight Pattern – Explained (3/3)<br />Useful in cases where large number of objects (flyweights)required to provide “fi...
Flyweight Pattern – UML Diagram<br />© Prafulla Paraskar 2010<br />
Flyweight Pattern – Real World Samples<br />Thumbnail representation of images in windows explorer. <br />Order processing...
Flyweight Pattern – Guidelines<br />When:<br /><ul><li>May objects to deal with in memory
Upcoming SlideShare
Loading in...5
×

Design Patterns - 03 Composite and Flyweight Pattern

4,014

Published on

Third presentation of the multi part series. This one covering composite and flyweight pattern.

Published in: Design
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,014
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Design Patterns - 03 Composite and Flyweight Pattern"

  1. 1. 03 Composite and Flyweight Pattern<br />Design Patterns<br />© Prafulla Paraskar 2010<br />
  2. 2. Composite<br />Structural Design Pattern<br />© Prafulla Paraskar 2010<br />
  3. 3. Definition<br />The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object.(wikipedia)<br />© Prafulla Paraskar 2010<br />
  4. 4. Composite Pattern – Explained (1/2)<br />© Prafulla Paraskar 2010<br />
  5. 5. Composite Pattern – Explained (2/2)<br />Arranges structured hierarchies so that single components and groups of components can be treated in the same way.<br />Makes easier to add new components.<br />Makes client code simpler, since they do not have to know if they are dealing with one or more components.<br />Typical operations on the components include add, remove, display, find and group.<br />© Prafulla Paraskar 2010<br />
  6. 6. Composite Pattern – UML Diagram<br />© Prafulla Paraskar 2010<br />
  7. 7. Composite Pattern – Real World Samples<br />Files and folders in Windows<br />Organization Tree<br />Arithmetic Expressions like (2 + 4) * ( 5 + 6)<br />Generic containers in .NET framework like list, queue, stack help implement the composite pattern.<br />© Prafulla Paraskar 2010<br />
  8. 8. Composite Pattern – Guidelines<br />You have :<br /><ul><li>An irregular structure of objects and composites of the objects.</li></ul>You Want<br /><ul><li>Clients to ignore all but the essential differences between objects and composites of objects.
  9. 9. To treat all objects in a composite uniformly.</li></ul>© Prafulla Paraskar 2010<br />
  10. 10. Flyweight<br />Structural Design Pattern<br />© Prafulla Paraskar 2010<br />
  11. 11. Definition<br />The Flyweight pattern is a pattern for sharing objects, where each instance does not contain its own state but stores it externally. This allows efficient sharing of objects to save space when there are many instances but only a few different types. (James W. Cooper – C# Design Patterns)<br />© Prafulla Paraskar 2010<br />
  12. 12. Flyweight Pattern – Explained (1/3)<br />Designing objects down<br /> to the lowest levels of<br /> system "granularity”<br /> provides optimal flexibility<br />, but can be unacceptably<br /> expensive in terms<br />Row objects<br />a<br />p<br />p<br />a<br />r<br />e<br />n<br />t<br />Row objects<br />Character object<br />Column object<br />© Prafulla Paraskar 2010<br />
  13. 13. Flyweight Pattern – Explained (2/3)<br />column<br />row<br />row<br />row<br />a<br />f<br />e<br />d<br />c<br />b<br />g<br />l<br />k<br />j<br />i<br />h<br />Flyweight pool<br />© Prafulla Paraskar 2010<br />
  14. 14. Flyweight Pattern – Explained (3/3)<br />Useful in cases where large number of objects (flyweights)required to provide “fine grain access” to the system. <br />Characteristics of flyweights are divided into intrinsic (inseparable) e.g. identity and extrinsic (separable) e.g. shared resource.<br />Shared resource is taken care by a directory which manages enrollment of participant and provides on demand access to shared resources.<br />Flyweights can only be created with help of a factory to enforce enrollment and sharing of resources. <br />© Prafulla Paraskar 2010<br />
  15. 15. Flyweight Pattern – UML Diagram<br />© Prafulla Paraskar 2010<br />
  16. 16. Flyweight Pattern – Real World Samples<br />Thumbnail representation of images in windows explorer. <br />Order processing system where the items are relatively similar and differ only in certain characteristics.<br />Representation of files and folders in windows explorer.<br />Individual cells in a spreadsheet program. <br />© Prafulla Paraskar 2010<br />
  17. 17. Flyweight Pattern – Guidelines<br />When:<br /><ul><li>May objects to deal with in memory
  18. 18. Different kinds of state, which can be handled differently to achieve space savings
  19. 19. Group of objects that share state
  20. 20. Ways of computing some of the state at runtime
  21. 21. The application doesn’t depend on object identity</li></ul>You want to:<br /><ul><li>Implement a system despite sever memory constraints</li></ul>© Prafulla Paraskar 2010<br />
  22. 22. Thank you!<br />http://geekswithblogs.net/technomad<br />Presentation by Prafulla Paraskar prafulla.paraskar@s1.com<br />© 2010 S1 India Pvt. Ltd.<br />Presentation by Prafulla Paraskar eprafulla@gmail.com<br />© 2010 Prafulla Paraskar<br />

×