Javatpoint Logo
Javatpoint Logo

Composite Design Pattern Java

The composite design pattern is a design pattern that allows us to arrange objects in a tree structure to represent a part-whole design. It allows customers to handle individual items and packages with precision. Simply put, it allows us to work with individual objects as well as sets of objects on a regular basis.

Features of Composite Design Pattern

Components:

It is an abstract class or interface that declares a common interface for all concrete classes. It can be an interface or an abstract class with methods that define generic operations.

leaf:

The leaf class represents individual objects that have no children. It uses the functionality defined by the Component interface.

Composite:

The Composite magnificence represents the composite items that may have children. It implements the operations declared by the Component interface and can also outline operations for adding and putting off children.

How does it work?

Composite pattern allows customers to handle individual items and packages in the same way. Customers can use the same types of functionality without having to distinguish between Leaf and Composite objects. It is achieved by defining a common interface (Component) and having it used by the Leaf and Composite classes.

Implementation of Composite Design Pattern in Java

Let's go through a simple example to illustrate the use of Composite Design Pattern in Java. Suppose, we want to model a corporate structure, where individual employees and departments can be treated equally.

Now, we can use the Composite pattern to create a company hierarchy that includes both individual employees and departments.

Client.java

Output:

Department: Tejas Corpo
Employee: Manoj Mamilla
Employee: Kamal bittu
Department: Engineering
Employee: Narasimha Rao
Employee: Manoj kumar
Department: Marketing
Employee: Pradeep marchi

Real-World Applications

Image systems

Consider a graphical layout in which we have to create shapes such as circles, triangles, and rectangles. Using a composite pattern allows us to treat individual shapes and compositions as one.

Client.java

Output:

Drawing Circle
Drawing Square

Advantages of Composite Designs Patterns

  • Similarities: Consumers can view individual products and packages as one.
  • Simplified customer rules: Clients do not need to distinguish between Leaf and Composite classes, resulting in simplified client code.
  • Simple modifications: The pattern allows new features to be added without modifying the existing client code.
  • Repeatable Scheduling: The model supports repetitive compositions, allows us to create complex layouts.

The composite design pattern in Java is a powerful tool for creating hierarchical structures that can be transformed into a composite. By understanding its components and usage, we can increase the flexibility, maintainability, and readability of the code. Whether we work with graphical systems, file systems, or any other application that involves part-whole hierarchies, Composite pattern is a valuable addition to our design pattern toolbox.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA