Composite Design Pattern in Java
Want to learn more about the composite design pattern in Java? Take a look at this tutorial to learn how to implement this design pattern in your project.
Join the DZone community and get the full member experience.Join For Free
Here I am with another useful design pattern for you — the Composite Design Pattern. I will try to point out the key features to remember while implementing the composite pattern for you.
Composite Design Pattern
The Composite Design Pattern is meant to "compose objects into a tree structure to represent part-whole hierarchies. Composite Pattern lets clients treat individual objects and compositions of objects uniformly".
- The Composite Design patterns describe groups of objects that can be treated in the same way as a single instance of the same object type.
- The Composite pattern allows us to "compose" objects into tree structures to represent part-whole hierarchies.
- In addition, the Composite patterns also allow our clients to treat individual objects and compositions in the same way.
- The Composite patterns allow us to have a tree structure for each node that performs a task.
- In object-oriented programming, a Composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality. This is known as a “has-a” relationship between objects.
Below is the list of classes/objects used in the composite pattern, which has four :
- Component – Component is the interface (or abstract class) for the composition of the objects and methods for accessing/processing its child or node components. It also implements a default interface to define common functionalities/behaviors for all component classes.
- Leaf – The leaf class defines a concrete component class, which does not have any further composition. The leaf class implements the component interface. It performs the command/task at its end only.
- Composite – The composite class defines a concrete component class, which stores its child components. The composite class implements the component interface. It forwards the command/task to the composite objects it contains. It may also perform additional operations before and after forwarding the command/task.
- Client – The client class uses the component interface to interact/manipulate the objects in the composition (Leaf and Composite).
To better understand this, let's take a look at an example of employees working in an organization.
- We create an interface to define functionalities we like to perform as composite and leaf objects. Below is the code of the
Workerinterface, which has methods for
Workinterface will act as a component of the composite pattern in the example.
The Work Class is defined as below:
To keep the example simple, Here I am dealing only with simple calculations like Factorial value and Palindrome and Armstrong check. Since Pandirome can be a string as well, I have kept work as String in the
Work class. Below class defines the algorithms/formulas to perform Factorial, Palindrome and Armstrong. Here's the code for Calculator enum:
I have created these algorithms as enums. To read more on Java-Enums, please refer to my article (Java Enums: How to Make Enums More Useful).
- We will create an abstract class of
Employeeto carry the common code for all various concrete subclasses of the employees.
Employeeclass declares abstract method teamSize() which I will use to split the work load assigned from a manager to an
Engineer. So teamSize() will will be 1 for
Engineer where as it will give the number of employees the
Manager is managing.
Engineer class is as below:
- We will create the
Managerclass to use as the composite object, and we will have another Employee object as a collection via the composition.
Manager class is as below:
- We define Work in the property file instead of hard-coding into the
work.propertiesis defined as below:
- The Main class uses
WorkLoaderclass to load work from the
WorkLoaderclass is defined as below:
- In the end, we will write the
Mainclass as the
Clientto execute and test our composite pattern code.
- Below is the output of the program:
Here, I haven't use concurrency to keep the example simple. Please feel free to enhance the example as per your choice at your end.
Source Code can be found here: Composite-Design-Pattern-Sample-Code
Liked this article? Don't forget to press that like button. Happy coding!
Need more articles on design patterns? Check out these useful posts:
- Null Object Pattern in Java
- Using the Adapter Design Pattern in Java
- Using the Bridge Design Pattern in Java
- Strategy vs. Factory Design Patterns in Java
- Decorator Design Pattern in Java
- How to Use Singleton Design Pattern in Java
- Singleton Design Pattern: Making Singleton More Effective in Java
- Java Enums: How to Make Enums More Useful
- Java Enums: How to Use Configurable Sorting Fields
Opinions expressed by DZone contributors are their own.