简体   繁体   中英

How Decorator Pattern Follows Composition

I read in HeadFirst DesginPattern book that decorator pattern uses composition relation to provide additional functionality to an object dynamically. But in the following code I found the aggregation relation instead of composition.

As far I know the difference between the composition and aggregation is:

Aggregation : Life or existence of the aggregated objects are independent of each other, But one object is playing the role of Owner of the other object.

Composition : Life or existence of the composite object is dependent on the existence of container object, Existence of composite object is not meaningful without its container object.

public abstract class Girl {

    String description = "no particular";

    public String getDescription(){
        return description;
    }
}

public class AmericanGirl extends Girl {

    public AmericanGirl(){
        description = "+American";
    }
}

public class EuropeanGirl extends Girl {

    public EuropeanGirl() {
        description = "+European";
    }
}

public abstract class GirlDecorator extends Girl {

    public abstract String getDescription();
}

public class Science extends GirlDecorator {

    private Girl girl;

    public Science(Girl g) {
        girl = g;
    }

    @Override
    public String getDescription() {
        return girl.getDescription() + "+Like Science";
    }

    public void caltulateStuff() {
        System.out.println("scientific calculation!");
    }
}

public class Main {

    public static void main(String[] args) {
        Girl g1 = new AmericanGirl();
        System.out.println(g1.getDescription()); 
        Science g2 = new Science(g1);
        System.out.println(g2.getDescription());        
    }
}

Can someone point out/explain how the above code follows composition?

The similarities between aggregation and composition make them often used as synonyms. IMO if you don't have a reason to make the distinction, it isn't worth the time debating on which it actually is. If you do have a reason, you probably already know which it falls under.

They are close enough in concepts that they are how you treat them. If you use the relationship as a single unit then it is a composition . If you interact with them independently of one another then it is an aggregation .

The major detail is in why it matters. The main need for a distinction between the two that I can think of is garbage collection. In an aggregation properties of the container may still be meaningful outside of their container context. In a composition properties of the container should be destroyed with the container.

When one observes the value of existence of an object inverse to the definition, I believe it can be expressed as both. In some circumstances you may wish to destroy only the container and retain the contents ( aggregation ). However, without it's delegate it is meaningless. This defines somewhat of an inverse composition . You would never want to destroy the child and retain the decorator.

Although Science is in fact a concrete Girl , the implementation of Science.getDescription overrides the base implementation to delegate the request to a concrete Girl . This implementation detail makes the Science girl meaningless without being initialized with a valid Girl implementation.

If the existence by itself is meaningless, then it usually isn't an aggregation and is probably instead a composition .

As an aside, decorators are more clearly expressed by implementing an interface rather than inheriting a base class. Adhering to the single responsibility principal you can then be certain that the decorator is not an implementation of the subject.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM