Composite Design Pattern

4,805
-1

Published on

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

No Downloads
Views
Total Views
4,805
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
105
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Composite Design Pattern

  1. 1. Composite Design Pattern <ul><ul><li>By </li></ul></ul><ul><ul><li>Ferdous Mahmud Shaon Software Engineer, </li></ul></ul><ul><ul><li>Escenic Bangladesh. </li></ul></ul>
  2. 2. Design Patterns All well-structured object-oriented systems are full of patterns . If the common collaborations among the objects of a complex object-oriented system is paid careful and sufficient attention, it can yield an architecture that is smaller, simpler and far more understandable Creational Structural Behavioral Abstract Factory Builder Prototype Singleton etc. Adapter Bridge Composite Decorator Façade Proxy etc. Command Iterator Observer Strategy Interpreter Mediator etc.
  3. 3. Structural Design Patterns <ul><li>Structural patterns are concerned with how classes and objects are composed to form larger structures… </li></ul><ul><li>Composite Design Pattern is a structural Design Pattern… </li></ul>May 23, 2007
  4. 4. What are Composite Objects ?? <ul><li>Objects that contain other objects; for example, In a graphics application, a drawing may be composed of graphic primitives, such as lines, circles, rectangles, text, and so on. </li></ul><ul><li>Menus that contain menu items, each of which could be a menu.  </li></ul><ul><li>Directories that contain files, each of which could be a directory.  </li></ul><ul><li>Containers that contain Elements, each of which could be a Container. </li></ul>May 23, 2007
  5. 5. Problems handling primitives and composites <ul><li>Application needs to manipulate a hierarchical collection of &quot;primitive&quot; and &quot;composite&quot; objects. </li></ul><ul><li>Processing of a primitive object is handled one way, and processing of a composite object is handled differently. </li></ul><ul><li>Having to query the &quot;type&quot; of each object before attempting to process it is not desirable . </li></ul>May 23, 2007
  6. 6. Why Composite Design Pattern ?? <ul><li>Ideally, we'd like to perform operations on both primitive objects and composites in exactly the same manner , without distinguishing between the two . </li></ul><ul><li>If we distinguish between primitive objects and composites to perform the same operations, our code would become more complex and more difficult to implement, and maintain. </li></ul>May 23, 2007
  7. 7. Why Composite Design Pattern ?? (cont.) <ul><li>Java developers need the Composite Design Pattern because we often want to manipulate composite objects exactly the same way , we manipulate primitive objects. </li></ul><ul><li>For example, graphic primitives such as lines or text must be drawn, moved, and resized. But we also want to perform the same operation on composites, such as drawings, that are composed of those primitives. </li></ul>May 23, 2007
  8. 8. Composite Pattern - Formal Definition <ul><li>Compose objects into tree structures to represent part-whole hierarchies . </li></ul><ul><li>Composite composes objects into tree structures and lets clients treat individual objects and compositions uniformly . </li></ul>May 23, 2007
  9. 9. A simple example May 23, 2007 Arithmetic expressions are Composites. An arithmetic expression consists of an operand, an operator ( +, -, *, / ) and another operand. The operand can be a number, or another arithmetic expression . Thus, 2 + 3 and (2 + 3) + (4 * 6) are both valid expressions.
  10. 10. Composite Pattern – Class Diagram May 23, 2007
  11. 11. Collaborations May 23, 2007 The clients handle a component through its general interface . A leaf directly executes the operations required, whereas a composite transfers the requests to its children.
  12. 12. An Example : File System <ul><li>class File { </li></ul><ul><li>private String mName; </li></ul><ul><li>public File( String pName ) { </li></ul><ul><li>mName = pName; </li></ul><ul><li>} </li></ul><ul><li>public void ls() { </li></ul><ul><li>System.out.println(mName +” ”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>class Directory { </li></ul><ul><li>private String mName; </li></ul><ul><li>private ArrayList mFiles; </li></ul><ul><li>public Directory( String pName ) { </li></ul><ul><li>mName = pName; </li></ul><ul><li>mFiles = new ArrayList(); </li></ul><ul><li>} </li></ul>May 23, 2007
  13. 13. Example Continued… May 23, 2007 public void ls() { System.out.println(mName + “ ” ); for (int i=0; i<mFiles.size(); i++) { Object obj = mFiles.get(i); /* **** Recover the type of this object ****** */ if (obj.getClass().getName().equals( &quot;Directory&quot; )) ((Directory) obj).ls(); else ((File) obj).ls(); } } /* -- Some other methods here including add() -- */ } So here, we had to query the &quot;type&quot; of each object before attempting to process it, but it is not desirable .
  14. 14. Can we use Composite Pattern here ?? May 23, 2007 The key to this composite pattern is an interface (AbstractFile) that represents both primitives(File) and composites (Directory).
  15. 15. Using Composite Pattern… <ul><li>Interface AbstractFile { </li></ul><ul><li>public void ls(); </li></ul><ul><li>}  </li></ul><ul><li>class File implements AbstractFile { </li></ul><ul><li>private String mName; </li></ul><ul><li>public File( String pName ) { </li></ul><ul><li>mName = pName; </li></ul><ul><li>} </li></ul><ul><li>public void ls() { </li></ul><ul><li>System.out.println( mName +” ”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>May 23, 2007
  16. 16. Using Composite Pattern… (cont.) <ul><li>class Directory implements AbstractFile { </li></ul><ul><li>private String mName; </li></ul><ul><li>private ArrayList mFiles; </li></ul><ul><li>public Directory( String pName ) { </li></ul><ul><li>mName = pName; </li></ul><ul><li>mFiles = new ArrayList(); </li></ul><ul><li>} </li></ul><ul><li>/* -- Some other methods here including add() -- */ </li></ul><ul><li>public void ls() { </li></ul><ul><li>System.out.println(mName + “ ” ); </li></ul><ul><li>for (int i=0; i<mFiles.size(); i++) { </li></ul><ul><li>AbstractFile abstarctFile = (AbstractFile) mFiles.get(i); </li></ul><ul><li>abstractFile.ls(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>May 23, 2007
  17. 17. Another Example – Graphics Drawing May 23, 2007 In graphics applications, the user can group components to form larger components
  18. 18. Some more examples <ul><li>Software System: A software system consists of subsystems which are either other subsystems or collection of classes. </li></ul><ul><li>Software Lifecycle: The software lifecycle consists of a set of development activities which are either other activities or collection of tasks. </li></ul><ul><li>Tiles in Struts framework </li></ul>May 23, 2007
  19. 19. Benefits <ul><li>Makes the clients simple . Clients can treat composite structures and individual objects uniformly. </li></ul><ul><li>Makes it easier to add new kind of components </li></ul><ul><li>Can make your design general. </li></ul>May 23, 2007
  20. 20. Some Implementation Issues <ul><li>Should we declare the child management operations ( addComponent(), removeComponent() ) in the component interface or composite class ?? </li></ul><ul><li>- Trade-off between safety and transparency </li></ul>May 23, 2007
  21. 21. Some Implementation Issues (cont.) <ul><li>Declaring the child management methods in the common interface of the primitive objects and composite objects gives you transparency , that is you can treat all components: primitives or composites uniformly . But it costs you safety , however, because clients may try to do meaningless things like add and remove components from leaves. </li></ul><ul><li>Defining the child management methods in the composite class gives you safety , since any attempt to add or remove components from leaves will be caught as an error at compile time. But you will lose transparency . </li></ul>May 23, 2007
  22. 22. Some Implementation Issues (cont.) <ul><li>Composite pattern is suitable for the applications where transparency is the main concern . </li></ul>May 23, 2007
  23. 23. Thank You
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×