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.
Scheme 1 - Starbucks Coffee resolve Order Item
Scheme 1 - Starbucks coffee in order to solve the problem 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, a method of abstract class Drink made.
-
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 an explosion class
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 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.
Scenario 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 may correspond to a return int
- Consider using the decorator pattern
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.
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) - Component body: for example, similar to the foregoing Drink
- ConcreteComponent and the Decorator
ConcreteComponent: specific subject, such as the front of each single coffee - 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.
Decorator Pattern solve Starbucks coffee orders
Under the orders of the decorator pattern: a + 2 parts chocolate milk LongBlack
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
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();
}
}