Composite Design Pattern in Java

The Composite Design Pattern is a structural design pattern that allows you to treat individual objects and compositions of objects uniformly. The Composite Design Pattern is used when you want to represent a hierarchical structure of objects, and you want to manipulate the structure as a whole as well as individual objects in the structure. The Composite pattern composes objects into tree-like structures to represent part-whole hierarchies.

The Composite Design Pattern in Java has the following properties:

  1. Hierarchical structure: The Composite Design Pattern represents a hierarchical structure of objects where objects can be composed of other objects, either leaf components or other composite components. The hierarchical structure is represented as a tree-like structure.
  2. Uniform interface: The Composite Design Pattern provides a uniform interface for all components, whether they are leaf components or composite components. This allows you to treat individual objects and compositions of objects uniformly.
  3. Recursive composition: The Composite Design Pattern allows you to compose objects recursively, meaning that composite objects can contain other composite objects as well as leaf objects.
  4. Flexibility: The Composite Design Pattern provides flexibility in creating complex objects by allowing you to compose objects in different ways.
  5. Simplifies client code: The Composite Design Pattern simplifies client code by allowing the client to treat a composite structure of objects as a single object. This makes it easier for the client to manipulate the composite structure.

In Java, the Composite Design Pattern can be implemented in the following way:

  1. Define a common interface or abstract class that represents the components of the composite structure.
    public interface Component {
        void operation();
    }
  2. Implement the interface or abstract class to create leaf components. These are the individual objects that will be part of the composite structure.
    public class Leaf implements Component {
        @Override
        public void operation() {
            System.out.println("Leaf operation");
        }
    }
  3. Implement the interface or abstract class to create composite components. These are the objects that contain other objects, either leaf components or other composite components.
    import java.util.ArrayList;
    import java.util.List;
    
    public class Composite implements Component {
        private List<Component> children = new ArrayList<>();
    
        public void add(Component component) {
            children.add(component);
        }
    
        public void remove(Component component) {
            children.remove(component);
        }
    
        @Override
        public void operation() {
            for (Component component : children) {
                component.operation();
            }
        }
    }

    In this implementation, the Composite class contains a list of Component objects, which can be leaf components or other composite components. The operation() method of the Composite class calls the operation() method of each component in the list.

  4. Use the Composite and Leaf classes to create a composite structure.
    public class Main {
        public static void main(String[] args) {
            Leaf leaf1 = new Leaf();
            Leaf leaf2 = new Leaf();
    
            Composite composite1 = new Composite();
            composite1.add(leaf1);
            composite1.add(leaf2);
    
            Leaf leaf3 = new Leaf();
    
            Composite composite2 = new Composite();
            composite2.add(composite1);
            composite2.add(leaf3);
    
            composite2.operation();
        }
    }

In this example, the composite structure is composed of three leaf components and two composite components. The composite structure is manipulated as a whole by calling the operation() method of the top-level composite component.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial