lundi 30 octobre 2017

Java: Decorator Pattern - reference to main abstract class

I have a question to the Decorator Pattern. I have read this article http://ift.tt/2bXolo8

Everything is clear to me, I only have one question:

This class inhertis from the abstract class SandWichDecorator:

public class CheeseDecorator extends SandWichDecorator{
Sandwich currentSandwich;

public CheeseDecorator(Sandwich sw){
    currentSandwich = sw;
}

@Override
public String getDescription(){
    return currentSandwich.getDescription() + ", Cheese";
}
@Override
public BigDecimal price() {
    return currentSandwich.price().add(new BigDecimal("0.50"));
} }

Why is the reference to the abstract class Sandwich "Sandwich currentSandwich;" in the CheeseDecorator class? Wouldn't it be easier to put it into the abstract class SandWichDecorator? And every class extending SandWichDecorator would automatically have the reference.

Thank you for your help, Peter

Aucun commentaire:

Enregistrer un commentaire