public abstract class CondimentDecorator extends Beverage{
public Beverage beverage;
public abstract String getDescription();
public Size getSize(){
return beverage.getSize( );
}
}
What is the reason for moving the instance variable beverage beverage from the condiment class to the abstract class CondimentDecorator?
Did they do this to save some lines of code, because now condiments don’t need to declare their own instance variables because they inherit it from the abstract class?
The UML diagram of the coffee shop before moving the instance variables:
Combination (not inheritance) It is the core of the decorator pattern, so it is very important to ensure that each decorator actually constitutes (decorates) its intended goal.
Ministry Design Patterns (2014, 2nd Edition)”, the author uses instance variable beverages from 4 condiment categories (milk, mocha, soy, whip) and puts them in the abstract class CondimentDecorator. (See the section 110 page, getSize() solution-problem)
public abstract class CondimentDecorator extends Beverage{
public Beverage beverage;
public abstract String getDescription();
public Size getSize(){
return beverage.getSize();
}
}
What is the reason why beverages moved from the condiment category to the abstract category CondimentDecorator?
Did they do this to save some lines of code, because now condiments don’t need to declare their own instance variables because they inherit it from the abstract class?
The UML diagram of the coffee shop before moving the instance variables:
More importantly, compared to reducing the code size, moving the Beverage instance to the CondimentDecorator ensures that each CondimentDecorator actually All have Beverage. Otherwise, someone can implement CondimentDecorator without a drink, which doesn’t make much sense.
Combination (rather than inheritance) is the core of the decorator pattern, so every decoration is guaranteed The actual composition (decoration) of the device is very important.