Composite Pattern
- 4th Apr 2018
- 0
- 5648
- Advantages of Composite Pattern composite pattern class diagram composite pattern in java composite pattern in java with examples Disadvantages of Composite Pattern how we can implement composite pattern Significance of composite pattern what is composite pattern ? When to use Composite Pattern ?
Composite Pattern is one of the structural design pattern
In this pattern, we will have a tree structure hierarchy of classes and common operation will be performed on each class in the hierarchy.
Node in the hierarchy is a class which will have children
Node class is also called as “Composite” class
Leaf in the hierarchy is a class which does not have children.
Both leaf class and the composite class share a common interface which defines the common operations that can be performed on both leaves and composites.
This Pattern is used to perform common operations on all the classes in the tree hierarchy
Composite Pattern pattern mainly consists of 4 parts
1) Component
2) Leaf
3) Composite/Node
4) Client
Component :
An interface which defines common methods for leaf and composites.
Leaf :
Implements component interface by defining the behaviour of elements in the composition
Composite :
It contains all the leaf objects and invoke the operations on each leaf object
Client :
The one who uses this composition through component interface
Let’s see how we can implement this pattern
Step 1
Create a node class/interface, which is a parent for all leaf classes
- package com.kb.composite;
- public interface AbstractOrder {
- public void calculateTotal();
- }
package com.kb.composite; public interface AbstractOrder { public void calculateTotal(); }
Step 2
Create leaf classes which becomes the children of Node class/interface
- package com.kb.composite;
- public class Cart implements AbstractOrder{
- @Override
- public void calculateTotal() {
- System.out.println("Cart total calculated");
- }
- }
package com.kb.composite; public class Cart implements AbstractOrder{ @Override public void calculateTotal() { System.out.println("Cart total calculated"); } }
- package com.kb.composite;
- public class Order implements AbstractOrder{
- @Override
- public void calculateTotal() {
- System.out.println("Order total calculated");
- }
- }
package com.kb.composite; public class Order implements AbstractOrder{ @Override public void calculateTotal() { System.out.println("Order total calculated"); } }
Step 3
Create a composite class which holds all the leaf classes and invokes operation on them
- package com.kb.composite;
- import java.util.ArrayList;
- import java.util.List;
- public class AbstractOrderComposite implements AbstractOrder {
- // collection of AbstractOrder
- private List<AbstractOrder> abstractOrderList = new ArrayList<AbstractOrder>();
- @Override
- public void calculateTotal() {
- for (AbstractOrder abstractOrder : abstractOrderList) {
- abstractOrder.calculateTotal();
- }
- }
- // adding abstractOrder to AbstractOrderComposite
- public void add(AbstractOrder abstractOrder) {
- abstractOrderList.add(abstractOrder);
- }
- // removing abstractOrder from AbstractOrderComposite
- public void remove(AbstractOrder abstractOrder) {
- abstractOrderList.remove(abstractOrder);
- }
- // removing all the abstractOrders in AbstractOrderComposite
- public void clear() {
- System.out.println("Clearing all the elements from composite class");
- abstractOrderList.clear();
- }
- }
package com.kb.composite; import java.util.ArrayList; import java.util.List; public class AbstractOrderComposite implements AbstractOrder { // collection of AbstractOrder private List<AbstractOrder> abstractOrderList = new ArrayList<AbstractOrder>(); @Override public void calculateTotal() { for (AbstractOrder abstractOrder : abstractOrderList) { abstractOrder.calculateTotal(); } } // adding abstractOrder to AbstractOrderComposite public void add(AbstractOrder abstractOrder) { abstractOrderList.add(abstractOrder); } // removing abstractOrder from AbstractOrderComposite public void remove(AbstractOrder abstractOrder) { abstractOrderList.remove(abstractOrder); } // removing all the abstractOrders in AbstractOrderComposite public void clear() { System.out.println("Clearing all the elements from composite class"); abstractOrderList.clear(); } }
Step 4
Create a client class which uses this pattern
- package com.kb.composite;
- public class Client {
- public static void main(String[] args) {
- AbstractOrder cart = new Cart();
- AbstractOrder order = new Order();
- AbstractOrderComposite abstractOrderComposite = new AbstractOrderComposite();
- abstractOrderComposite.add(cart);
- abstractOrderComposite.add(order);
- abstractOrderComposite.calculateTotal();
- //This will remove all elements which composite class holds
- abstractOrderComposite.clear();
- }
- }
package com.kb.composite; public class Client { public static void main(String[] args) { AbstractOrder cart = new Cart(); AbstractOrder order = new Order(); AbstractOrderComposite abstractOrderComposite = new AbstractOrderComposite(); abstractOrderComposite.add(cart); abstractOrderComposite.add(order); abstractOrderComposite.calculateTotal(); //This will remove all elements which composite class holds abstractOrderComposite.clear(); } }
Now if we observe abstractOrderComposite.calculateTotal(); line in the above class
We can see that, this single line is calling methods of each leaf class as operation is same for all the leaf class but implementation is different for each leaf class.
Applications of Composite Pattern
This pattern is already used in many places within java itself like XML parsing and in some of the collection methods like
List#addAll(Collection), Set#addAll(Collection) etc
Advantages of Composite Pattern
Clients use the Component class interface to interact with objects in the composite structure
Calling a Composite forwards, the request to its child components
Disadvantages of Composite Pattern
Leaf classes have to create some methods which has to empty in some cases
Once tree structure is defined, the composite pattern makes the tree overly general
When to use Composite Pattern ?
1. When we need to treat all objects in the composite structure uniformly.
2. When the group of objects should behave as the single object.