Design Mode – What is the reason for the abstract class that moves instance variables to the decorator mode?

In the book “Head Design Patterns (2014, 2nd Edition)” in the chapter about decorator patterns, the author chooses four types of condiments (milk, mocha, soy , Whip) and put it in the class CondimentDecorator. (See page 110, 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 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:

 UML for coffee shop

More importantly, compared to reducing the code size, the Beverage Moving the instance into CondimentDecorator ensures that each CondimentDecorator actually has Beverage. Otherwise, someone can implement CondimentDecorator without a drink, which doesn’t make much sense.

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:

 UML for coffee shop

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.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4249 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.