What is the reason for moving an instance variable to an abstract class in a decorator template?

In the book "Head first design patterns (2014, 2nd edition)" in the chapter on the decorator template, the authors take an instance variable Beverage beveragefrom 4 seasoning classes (Milk, Mocha, Soy, Whip) and put it in an abstract class CondimentDecorator. (see page 110, getSize()-question solution )

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 an instance variable Beverage beveragefrom condiment classes to an abstract class CondimentDecorator?

They do this to save some lines of code, since now condiments do not need to declare their own instance variable, since they inherit it from an abstract class?

UML diagram for coffe store BEFORE instance variable engine:

UML for cafe

+4
3

, , Beverage CondimentDecorator , CondimentDecorator Beverage. - CondimentDecorator Beverage, .

( ) - , , () .

+3

Design Pattern : Decorator / .

- :

  • Caffe Mocha, , .
  • Caffe Mocha, , .
  • .. ..

/ Beverage, Beverages Milk, Mocha, Soy, Whip. Beverage: - Beverage, Beverage.

, Beverage, Beverage / ( Beverages) Beverage, .

, Beverage: "Caffe Mocha, soy milk, with whipped cream". Mocha. Soy , Soy Whip . , Soy Whip Mocha.

Soy -> Whip

Mocha -> Soy -> Whip

, Beverage, Beverage Beverage, Beverage Beverage.

, , . , / Beverage , Beverage , . , , /Beverage. Decorator.

0

Wiki:

Decorator ( Wrapper, , ) , , , .

Decorator - . , AuthenticationService, authenticate() . . . LoggingSupportAuthenticationService AuthenticationService authenticate(), AuthenticationService .

I do not have any code editor, so you can read this article for more information: https://msdn.microsoft.com/en-us/library/dn178467(v=pandp.30).aspx#sec3

0
source

All Articles