Composite Design Pattern By Ferdous Mahmud Shaon  Software Engineer, Escenic Bangladesh.
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.
Structural  Design  Patterns Structural patterns are concerned with how classes and objects are composed to form  larger  structures… Composite Design Pattern is a structural Design Pattern… May 23, 2007
What are Composite Objects ?? 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. Menus  that contain menu items, each of which could be a menu.  Directories  that contain files, each of which could be a directory.  Containers  that contain Elements, each of which could be a Container. May 23, 2007
Problems handling primitives and composites Application needs to manipulate a hierarchical collection of "primitive" and "composite" objects.  Processing of a primitive object is handled one way, and processing of a composite object is handled differently.  Having to query the  "type"  of each object before attempting to process  it  is  not desirable . May 23, 2007
Why Composite Design Pattern ?? Ideally, we'd like to perform operations on both primitive objects and composites in exactly the  same manner , without distinguishing between the two .   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. May 23, 2007
Why Composite Design Pattern ?? (cont.) Java developers need the Composite Design Pattern because we often want to manipulate composite objects exactly the  same way ,  we manipulate primitive objects.  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.  May 23, 2007
Composite Pattern - Formal Definition Compose objects into tree structures to represent  part-whole hierarchies .  Composite composes objects into tree structures and lets clients treat individual objects and compositions  uniformly .  May 23, 2007
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.
Composite Pattern – Class Diagram May 23, 2007
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.
An Example : File System class  File  { private String mName;  public File( String pName ) {  mName = pName; } public void ls() { System.out.println(mName +”  ”); } } class  Directory  { private String  mName; private ArrayList mFiles; public Directory( String pName ) { mName = pName; mFiles = new ArrayList(); } May 23, 2007
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 .
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).
Using Composite Pattern… Interface  AbstractFile   {  public void ls(); }  class  File  implements  AbstractFile   {  private String mName; public File( String pName ) {  mName = pName;  }  public void ls()  {  System.out.println( mName +”  ”);  } } May 23, 2007
Using Composite Pattern… (cont.) class  Directory   implements AbstractFile  { private String  mName; private ArrayList mFiles; public Directory( String pName ) { mName = pName; mFiles = new ArrayList(); } /* -- Some other methods here including add() -- */  public void ls() { System.out.println(mName + “  ” ); for (int i=0; i<mFiles.size(); i++) { AbstractFile abstarctFile = (AbstractFile) mFiles.get(i);  abstractFile.ls(); } } } May 23, 2007
Another Example – Graphics Drawing May 23, 2007 In graphics applications, the user can group components to form larger components
Some more examples Software System:  A software system consists of subsystems which are either other subsystems or collection of classes. Software Lifecycle:  The software lifecycle consists of a set of development activities which are either other activities or collection of  tasks. Tiles  in Struts framework  May 23, 2007
Benefits Makes the clients simple . Clients can treat composite structures and individual objects uniformly. Makes it easier to add new kind of components Can make your design general. May 23, 2007
Some Implementation Issues Should we declare the child management operations ( addComponent(), removeComponent() ) in the component interface or composite class ??  - Trade-off between  safety  and  transparency May 23, 2007
Some Implementation Issues (cont.) 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. 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 .  May 23, 2007
Some Implementation Issues (cont.) Composite pattern is suitable for the applications where  transparency is the main concern . May 23, 2007
Thank You

Composite Design Pattern

  • 1.
    Composite Design PatternBy Ferdous Mahmud Shaon Software Engineer, Escenic Bangladesh.
  • 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.
    Structural Design Patterns Structural patterns are concerned with how classes and objects are composed to form larger structures… Composite Design Pattern is a structural Design Pattern… May 23, 2007
  • 4.
    What are CompositeObjects ?? 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. Menus that contain menu items, each of which could be a menu.  Directories that contain files, each of which could be a directory.  Containers that contain Elements, each of which could be a Container. May 23, 2007
  • 5.
    Problems handling primitivesand composites Application needs to manipulate a hierarchical collection of &quot;primitive&quot; and &quot;composite&quot; objects. Processing of a primitive object is handled one way, and processing of a composite object is handled differently. Having to query the &quot;type&quot; of each object before attempting to process it is not desirable . May 23, 2007
  • 6.
    Why Composite DesignPattern ?? Ideally, we'd like to perform operations on both primitive objects and composites in exactly the same manner , without distinguishing between the two . 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. May 23, 2007
  • 7.
    Why Composite DesignPattern ?? (cont.) Java developers need the Composite Design Pattern because we often want to manipulate composite objects exactly the same way , we manipulate primitive objects. 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. May 23, 2007
  • 8.
    Composite Pattern -Formal Definition Compose objects into tree structures to represent part-whole hierarchies . Composite composes objects into tree structures and lets clients treat individual objects and compositions uniformly . May 23, 2007
  • 9.
    A simple exampleMay 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.
    Composite Pattern –Class Diagram May 23, 2007
  • 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.
    An Example :File System class File { private String mName; public File( String pName ) { mName = pName; } public void ls() { System.out.println(mName +” ”); } } class Directory { private String mName; private ArrayList mFiles; public Directory( String pName ) { mName = pName; mFiles = new ArrayList(); } May 23, 2007
  • 13.
    Example Continued… May23, 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.
    Can we useComposite Pattern here ?? May 23, 2007 The key to this composite pattern is an interface (AbstractFile) that represents both primitives(File) and composites (Directory).
  • 15.
    Using Composite Pattern…Interface AbstractFile { public void ls(); }  class File implements AbstractFile { private String mName; public File( String pName ) { mName = pName; } public void ls() { System.out.println( mName +” ”); } } May 23, 2007
  • 16.
    Using Composite Pattern…(cont.) class Directory implements AbstractFile { private String mName; private ArrayList mFiles; public Directory( String pName ) { mName = pName; mFiles = new ArrayList(); } /* -- Some other methods here including add() -- */ public void ls() { System.out.println(mName + “ ” ); for (int i=0; i<mFiles.size(); i++) { AbstractFile abstarctFile = (AbstractFile) mFiles.get(i); abstractFile.ls(); } } } May 23, 2007
  • 17.
    Another Example –Graphics Drawing May 23, 2007 In graphics applications, the user can group components to form larger components
  • 18.
    Some more examplesSoftware System: A software system consists of subsystems which are either other subsystems or collection of classes. Software Lifecycle: The software lifecycle consists of a set of development activities which are either other activities or collection of tasks. Tiles in Struts framework May 23, 2007
  • 19.
    Benefits Makes theclients simple . Clients can treat composite structures and individual objects uniformly. Makes it easier to add new kind of components Can make your design general. May 23, 2007
  • 20.
    Some Implementation IssuesShould we declare the child management operations ( addComponent(), removeComponent() ) in the component interface or composite class ?? - Trade-off between safety and transparency May 23, 2007
  • 21.
    Some Implementation Issues(cont.) 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. 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 . May 23, 2007
  • 22.
    Some Implementation Issues(cont.) Composite pattern is suitable for the applications where transparency is the main concern . May 23, 2007
  • 23.