1

For the below relation between Container and Componentin java.awt class hierarchy, I see that,

enter image description here

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.

enter image description here 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

overexchange
  • 2,245
  • 2
  • 17
  • 47

2 Answers2

2

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 Pattern

In 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.


foreach o in children, o.operation()

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.

cbojar
  • 4,211
  • 1
  • 17
  • 18
  • This reminds me of a gag I saw on The Simpsons... A truck truck truck http://pics.imcdb.org/0is259/snapshot20070219003351gg4.4301.jpg – toniedzwiedz Nov 29 '14 at 12:37
  • @toniedzwiedz Lol. Composite pattern to the extreme! – cbojar Nov 29 '14 at 16:55
  • i think this is what you are trying to [picturize](https://github.com/shamhub/books/blob/master/java-Preparation/main_docs/design_pattern/composite_pattern.png) from your example code. So, can i say, `public class CarCarrier extends ContainerVehicle`, because awt `Frame` extends `Container` but not `Component` directly. looks like composit_pattern is least used in real world programming, i could not see anything around me that fits as composite pattern.Perfect answer!!!! – overexchange Dec 05 '14 at 07:24
1

The composite pattern involves a parent that has a set of child objects (all children and the parent have some interface or superclass in common), and one (or more) operations that are implemented by delegating to the same operation in the children, potentially with some additional operations performeda as well.

In the case of Component, the delegated operations are event handling, painting and so on. For instance,, the implementation of paint is to loop over the children, adjust the origin and clipping region of the Graphics object for each, and call its paint method.

Jules
  • 17,614
  • 2
  • 33
  • 63