Design Patterns @ Chapter 11: Decorator design pattern

Chapter 11: Decorator design pattern

A Starbucks coffee order item (Cafe):

  • Types of coffee / single coffee: Espresso (espresso), ShortBlack, LongBlack (American coffee), Decaf (no decaf)
  • Seasoning: Milk, Soy (soy milk), Chocolate
  • Required when extending new coffee species, has good scalability, easy to change, easy to maintain
  • OO is calculated using the cost of different types of coffee: Customers can order single coffee, coffee can be a single product + spices combination.

Second, the program 1- solve Starbucks coffee Order Item

Equivalent lists all permutations.

Order a coffee program

Third, resolve Scheme 1 - Starbucks Coffee Order Analysis

  • Drink is an abstract class that represents beverages
  • des is a description of coffee, such as coffee names
  • cost () method is to calculate the cost, Drink made an abstract class method
  • Decaf coffee is a single product, inheritance Drink, and achieve cost
  • Espress && Milk is the single coffee + seasoning, this combination of a lot of

    Question: this design, there will be a lot of class, when we add a single coffee, or a new sauce, the number of classes will be doubled, there will be class explosion

Fourth, Option 2 - resolve Starbucks coffee orders (good point)

  • Analysis of the foregoing embodiment 1 because a single product coffee + seasoning combinations result multiplied class, it is possible to make improvements, seasoning (attributes) Drink built into the class, so as not to cause excessive number of classes. Thereby enhancing the maintenance of the project (Figure)

  • Description: milk, soy, chocolate can be designed as a Boolean, indicating whether you want to add the appropriate spices.

Coffee Order Option II

V. Option 2 - Starbucks coffee in order to solve the problem analysis

  • Scenario 2 can control the number of classes, and will not cause a lot of class
  • When adding or deleting kind of spices, a large amount of maintenance code
  • When considering that the user can add more than seasoning, hasMilk () method may return corresponding int
  • Consider using the decorator pattern

Sixth, the decorator pattern definitions

  • Decorator: dynamic new functionality attached to the object . In the object function expansion, it is more flexible than inheritance, the decorator pattern also reflects the principle of opening and closing (ocp)

  • Mentioned here dynamic new functionality ocp attached to the object and principles will be embodied in the form of code on the back of the application example, Please pay attention to experience.

Seven, the decorator pattern principle

  • The decorator pattern is like a courier package
    body: such as: ceramics, clothing (Component) // be the decorator
    packaging: such as: newspapers filled, plastic foam, cardboard, wood (Decorator) // decorator

  • Component body: for example, similar to the foregoing Drink
  • ConcreteComponent and Decorator ConcreteComponent: specific subject, such as the front of each single coffee
  • Decorator: decorators, such as various spices.

    FIG Component and between the ConcreteComponent, if many ConcreteComponent class, also can design a buffer layer, the common portion is extracted, a layer of abstraction class.

Decorator Pattern class diagram principle

Decorator Pattern solve Starbucks coffee orders

Decorator Pattern Coffee solve the problem

Under the orders of (a) the decorator pattern: a + 2 parts chocolate milk LongBlack

Under the orders of the decorator pattern

(B) the decorator pattern coffee Order Item application examples

First Drink abstract class:

package com.gjxaiou.decorator;

public abstract class Drink {

    public String des; // 描述
    private float price = 0.0f;
    public String getDes() {
        return des;
    }
    public void setDes(String des) {
        this.des = des;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    
    //计算费用的抽象方法
    //子类来实现
    public abstract float cost();
}

Coffee is then used as a buffer type, to facilitate later expanded:

package com.gjxaiou.decorator;

public class Coffee  extends Drink {
    @Override
    public float cost() {
        return super.getPrice();
    }
}

The following are specific entity classes: single coffee

package com.gjxaiou.decorator;

public class ShortBlack extends Coffee{
    
    public ShortBlack() {
        setDes(" shortblack ");
        setPrice(4.0f);
    }
}

//---------------------------------------------------
package com.gjxaiou.decorator;

public class DeCaf extends Coffee {

    public DeCaf() {
        setDes(" 无因咖啡 ");
        setPrice(1.0f);
    }
}

//--------------------------------------------------
package com.gjxaiou.decorator;

public class Espresso extends Coffee {
    
    public Espresso() {
        setDes(" 意大利咖啡 ");
        setPrice(6.0f);
    }
}

//-------------------------------------------------------
package com.gjxaiou.decorator;

public class LongBlack extends Coffee {

    public LongBlack() {
        setDes(" longblack ");
        setPrice(5.0f);
    }
}

Then the decorator pattern:

Its specific category:

package com.gjxaiou.decorator;

//具体的Decorator, 这里就是调味品
public class Chocolate extends Decorator {

    public Chocolate(Drink obj) {
        super(obj);
        setDes(" 巧克力 ");
        setPrice(3.0f); // 调味品 的价格
    }
}

//------------------------------------------
package com.gjxaiou.decorator;

public class Milk extends Decorator {

    public Milk(Drink obj) {
        super(obj);
        setDes(" 牛奶 ");
        setPrice(2.0f); 
    }
}

//-------------------------------------------
package com.gjxaiou.decorator;

public class Soy extends Decorator{

    public Soy(Drink obj) {
        super(obj);
        setDes(" 豆浆  ");
        setPrice(1.5f);
    }
}
package com.gjxaiou.decorator;

public class Decorator extends Drink {
    private Drink obj;
    
    public Decorator(Drink obj) { //组合
        this.obj = obj;
    }
    
    @Override
    public float cost() {
        // getPrice 自己价格
        return super.getPrice() + obj.cost();
    }
    
    @Override
    public String getDes() {
        // obj.getDes() 输出被装饰者的信息
        return des + " " + getPrice() + " && " + obj.getDes();
    }
}

XI decorator pattern in JDK source code analysis application

Java's IO structure, FilterInputStream is a decorator

FIG decorator pattern class JavaIO

Guess you like

Origin www.cnblogs.com/qq438649499/p/12178307.html