Composite is a type of structural design pattern that allows us to compose objects into tree-like structures and then operate with these structures as if they were single objects. It similarly treats collections of objects as a single instance of the same type. The primary purpose of the composite method is to compose objects into a tree-like structure so as to be able to work with these structures as an individual object.
The composite method is to compose objects into a tree-like structure so as to be able to work with these structures as an individual object, but this won’t be possible without the explanation of the participants in the composite method. These participants are four in number, and they are:
- a client is used to manipulate the objects in the composition through the component interface.
- a component helps in using the default character for the interface similar to all classes.
- it keeps the child component and implements the child-related operations in the component interface.
- it explains the behaviour of the primitive objects in the composition.
The composite method can be implemented using the following guidelines:
- Be sure that the app you are to build can be well represented in a tree-like structure. Try to break the app component into simple elements and containers. The containers must be able to contain both simple elements and other containers.
- Define the component interface with a list of methods that make sense for both simple and complex components.
- Create a leaf class that represents simple elements because a program may have multiple leaf classes that are different.
- Create a container that represents complex elements
- Lastly, define the methods for adding and removing the child elements in the container.
For example, We attempt to make a school system hierarchy with the subclasses like:
So, The composite method will be used to implement the above hierarchy using python.
class LeafElement: def __init__(self, *args): self.position = args def showDetails(self): print("\t") print(self.position) class CompositeElement: def __init__(self, args): self.position = args self.children =  def add(self, child): self.children.append(child) def remove(self, child): self.children.remove(child) def showDetails(self): print(self.position) for child in self.children: print("\t") child.showDetails() if __name__ == '__main__': # Adding the functions of each objects TheProvisionalSuperiror = CompositeElement(" - TheProvisionalSuperiror") SuperirorLisieux = CompositeElement(" - Superiror Lisieux Convent") ThePrincipal = CompositeElement(" - The Principal") HTUpper = CompositeElement(" - Head Teacher(Upper Classes)") HTBaby = CompositeElement(" - Head Teacher(Baby Classes)") HTPlay = CompositeElement(" - Head Teacher(Play Group)") Teachers = LeafElement(" - Teachers") Dance_Music = LeafElement(" - Dance and Music") SupportinStaff = LeafElement(" - Supporting Staff") # Implementation HTUpper.add(Teachers) HTBaby.add(Teachers) HTPlay.add(Teachers) ThePrincipal.add(HTUpper) ThePrincipal.add(HTBaby) ThePrincipal.add(HTPlay) ThePrincipal.add(Dance_Music) ThePrincipal.add(SupportinStaff) SuperirorLisieux.add(ThePrincipal) TheProvisionalSuperiror.add(SuperirorLisieux) TheProvisionalSuperiror.showDetails()
ProBook:~/InfosecAddicts/python design pattern$ python composite.py - TheProvisionalSuperiror - Superiror Lisieux Convent - The Principal - Head Teacher(Upper Classes) - Teachers - Head Teacher(Baby Classes) - Teachers - Head Teacher(Play Group) - Teachers - Dance and Music - Supporting Staff
Pros and Cons
- Fast Execution Time
- Less Memory Consumption
- Open/Close Principle
- Component Restrictions
- General Tree Structure