We are moved to new domain
Click -> www.ehowtonow.com
Wednesday, 26 July 2017

Design Patterns - Composite Pattern

Composite Pattern

Definition

The Composite pattern helps you to create tree structures of objects without the need to force clients to differentiate between branches and leaves regarding usage. The Composite pattern lets clients treat individual objects and compositions of objects uniformly.

Where to use

•When you want to represent a part-whole relationship in a tree structure.
•When you want clients to be able to ignore the differences between compositions of objects and individual objects.
•When the structure can have any level of complexity and is dynamic.

Benefits

•Define class hierarchies consisting of primitive objects and composite objects.
•Makes it easier to add new kind of components.

Drawbacks/consequences

The Composite pattern makes it easy for you to add new kinds of components to your collection as long as they support a similar programming interface. On the other hand, this has the disadvantage of making your system overly general. You might find it harder to restrict certain classes where this would normally be desirable.

Composite Pattern Class Diagram


In the class-diagram above, Component:
•is the abstraction for all components, including composite ones,
•declares the interface for objects in the composition,
•implements default behavior for the interface common to all classes, as appropriate,
•declares an interface for accessing and managing its child components, and
•(optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate.

In the class-diagram above, Leaf:
•represents leaf objects in the composition, and
•implements all Component methods.

In the class-diagram above, Composite:
•represents a composite Component (component having children),
•implements methods to manipulate children, and
•implements all Component methods, generally by delegating them to its children.

Composite Pattern Example

package com.designpattern.composite;
public interface AbstractFile {
public void ls();
}
package com.designpattern.composite;
import java.util.ArrayList;
public class Directory implements AbstractFile {
private String name;
private ArrayList files = new
ArrayList();
private Indentation indentation;
public Directory (String name, Indentation indentation) {
this.name = name;
this.indentation = indentation;
}
public void add(AbstractFile f) {
files.add(f);
}
public void ls() {
System.out.println(indentation.getIndentation() + name);
indentation.increaseIndentation();
for (AbstractFile file : files) {
file.ls();
}
indentation.decreaseIndentation();
}
}
package com.designpattern.composite;
class File implements AbstractFile {
private String name;
private Indentation indentation;
public File(String name, Indentation indentation) {
this.name = name;
this.indentation = indentation;
}
public void ls() {
System.out.println(indentation.getIndentation() +
name);
}
}

package com.designpattern.composite;
public class Indentation {
private StringBuffer sbIndent = new StringBuffer();
public String getIndentation() {
return sbIndent.toString();
}
public void increaseIndentation() {
sbIndent.append(" ");
}
public void decreaseIndentation() {
if (sbIndent.length() >= 3) {
sbIndent.setLength(sbIndent.length() - 3);
}
}
}
package com.designpattern.composite;
public class CompositeDemo {
public static void main(String[] args) {
Indentation indentation = new Indentation();
Directory dirOne = new Directory("dir111", indentation);
Directory dirTwo = new Directory("dir222", indentation);
Directory dirThree = new Directory("dir333",
indentation);
File a = new File("a", indentation);
File b = new File("b", indentation);
File c = new File("c", indentation);
File d = new File("d", indentation);
File e = new File("e", indentation);
dirOne.add(a);
dirOne.add(dirTwo);
dirOne.add(b);
dirTwo.add(c);
dirTwo.add(d);
dirTwo.add(dirThree);
dirThree.add(e);
dirOne.ls();
}
}
When CompositeDemo is executed the result is:
c:>dir111
c:> a
c:> dir222
c:> c
c:> d
c:> dir333
c:>
c:> e
c:>
c:> b
Below is the class-diagram for the small example.



Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Composite Pattern Rating: 5 Reviewed By: eHowToNow