Design Patterns notes seven decorator pattern

Starbucks coffee order item (Cafe):

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

Scheme 1 - Starbucks Coffee resolve Order Item

Here Insert Picture Description

Scheme 1 - Starbucks coffee in order to solve the problem analysis

  1. Drink is an abstract class that represents beverages

  2. des is a description of coffee, such as coffee names

  3. cost () method is to calculate the cost, a method of abstract class Drink made.

  4. Decaf coffee is a single product, inheritance Drink, and achieve cost

  5. Espress && Milk is the single coffee + seasoning, this combination of a lot of

  6. 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 an explosion class

Option 2 - resolve Starbucks coffee orders (good point)

Here Insert Picture Description

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

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

Scenario 2 - Starbucks coffee in order to solve the problem analysis

  1. Scenario 2 can control the number of classes, and will not cause a lot of class
  2. When adding or deleting kind of spices, a large amount of maintenance code
  3. When considering that the user can add more than seasoning, hasMilk may correspond to a return int
  4. Consider using the decorator pattern

The decorator pattern definitions

  1. 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)
  2. 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.

The decorator pattern principle

  1. 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)
  2. Component body: for example, similar to the foregoing Drink
  3. ConcreteComponent and the Decorator
    ConcreteComponent: specific subject, such as the front of each single coffee
  4. Decorator:. Decorators, various spices such as
    between Component and Fig ConcreteComponent, if many ConcreteComponent class, also can design a buffer layer, the common portion is extracted, a layer of abstraction class.
    Here Insert Picture Description

Decorator Pattern solve Starbucks coffee orders

Here Insert Picture Description

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

Here Insert Picture Description

Decorator Pattern Coffee Order Item application examples

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();
	
}

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

}

Decorator Pattern JDK source code analysis applications

Java's IO structure, FilterInputStream is a decorator
Here Insert Picture Description

import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputStream;

public class Decorator {


public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub

//说明
//1. InputStream  是抽象类,  类似我们前面讲的 Drink
//2. FileInputStream 是	InputStream  子类,类似我们前面的 DeCaf, LongBlack
//3. FilterInputStream	是	InputStream 子类:类似我们前面 的 Decorator 修饰者
//4. DataInputStream  是 FilterInputStream  子类,具体的修饰者,类似前面的 Milk, Soy 等
//5. FilterInputStream 类  有	protected volatile InputStream in;  即含被装饰者
//6. 分析得出在 jdk 的 io 体系中,就是使用装饰者模式


DataInputStream dis = new DataInputStream(new FileInputStream("d:\\abc.txt")); System.out.println(dis.read());
dis.close();
}


}

Published 93 original articles · won praise 31 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43866567/article/details/104753887