For the below relation between Container and Componentin java.awt class hierarchy, I see that,in addition to is-a relation, class Container has composite relation with class Component with below line of code,/**      * The components in this container.     * @see #add     * @see #getComponents     */    private java.util.List<Component> component = new java.util.ArrayList<Component>();But am still not clear with the below diagram wrt operation() method. So, 1)What does class Leaf and class Composite signify in the below diagram? 2)What exactly are we depicting about operation() method in class Leaf and class Composite as per the below diagram?Note: I am java beginner learning java
Query on Recursive composition
java;object oriented;awt
What you are looking at in the first diagram is how the AWT classes in Java use the Composite Pattern so that they are able to compose together elements in a larger element of the same type. The second diagram shows the generalized form of the Composite Pattern.The Composite PatternIn order to understand the composite pattern, let's use a more concrete example for a better perspective. First, we have a super class (could also be an interface) Vehicle for things that drive on the road, like cars, trucks, 18-wheelers, etc:public abstract class Vehicle{     public void drive(int distance) {        System.out.println(Drove  + distance + . Vroom vroom!);     }     public abstract int getWeight();     // etc.}Now we also have a concrete class for a car:public class Car extends Vehicle{    protected int weight;    // ...    @Override    public int getWeight() {        return this.weight;    }}We also have a class for a car carrier, which is a vehicle that can contain other vehicles:public class CarCarrier extends Vehicle{    protected int weight;    protected Vehicle[] cars;    // ...    @Override    public int getWeight() {        int weight = this.weight;        for(Vehicle car : cars) {            weight += car.getWeight();        }        return weight;    }}As we can see in the getWeight method of CarCarrier, we have to calculate the weight of the car carrier as its weight plus the weight of the cars it carries. Thus the getWeight method of CarCarrier calls the getWeight method of each of the child Vehicles in the cars array. The Car class corresponds to the leaf, and the CarCarrier class corresponds to the composite.The above formula, describing how the composite's operation method should work, basically translates into for each child, we'll call it 'o', in children, we have to call the operation method on o when the composite's operation method is called. This corresponds to how we called the getWeight method of each of the Vehicles being carried by the CarCarrier to get the total weight of the CarCarrier.