What is Composite Design Pattern
Composite Design pattern is ideal for designing a system where part-whole hierarchies exist, and where the part and the whole components are to be treated in the system uniformly.
Composite design pattern treats both the parts and the whole in the same way. This is very beneficial for handling hierarchical part-whole hierarchies which are recursive in nature.
Consider a binary tree. The left sub-tree and right sub-tree are themselves full-fledged binary trees. Traversals of these trees is recursive in nature due to this part-whole similarity.
Another ubiquitous example is a UML editor, every element of the diagram is treated(drawn) in the editor the same way as the whole UML diagram.
Composite Design pattern is ideal for designing a system where part-whole hierarchies exist, and where the part and the whole components are to be treated in the system uniformly.
Composite design pattern treats both the parts and the whole in the same way. This is very beneficial for handling hierarchical part-whole hierarchies which are recursive in nature.
Consider a binary tree. The left sub-tree and right sub-tree are themselves full-fledged binary trees. Traversals of these trees is recursive in nature due to this part-whole similarity.
Another ubiquitous example is a UML editor, every element of the diagram is treated(drawn) in the editor the same way as the whole UML diagram.
Composite Pattern consists of following objects.
- Base Component – Base component is the interface for all objects in the composition, client program uses base component to work with the objects in the composition. It can be an interface or an abstract class with some methods common to all the objects.
- Leaf – Defines the behaviour for the elements in the composition. It is the building block for the composition and implements base component. It doesn’t have references to other Components.
- Composite – It consists of leaf elements and implements the operations in base component.
References:
http://www.javabrahman.com/design-patterns/composite-design-pattern-in-java/
http://www.journaldev.com/1535/composite-design-pattern-in-java
No comments:
Post a Comment