文章目录
一、星巴克咖啡订单项目
- 咖啡种类/单品咖啡:Espresso(意大利浓咖啡),ShrotBlack,LongBlack,Decaf
- 调料Milk,soy,chocolate
- 要求在扩展新的咖啡种类时,具有良好的扩展性,改动方便,维护方便
- 使用oo的来计算不同种类咖啡的费用:客户可以点单品咖啡,也可以单品咖啡+调料组合
二、方案一
1.问题分析
- Drink是一个抽象类,表示饮料
- des就是对咖啡的描述,比如咖啡的名字
- cost()方法就是计算费用,Drink类中做成一个抽象方法
- Decaf就是单品咖啡,继承Drink,并实现cost
- Espress&&milk就是单品咖啡+调料,这个组合很多
- 问题:这样设计,会有很多类,当我们增加一个单品咖啡,或者一个新的调料,类的数量就会倍增,就会出现类爆炸
三、方案二
1.问题分析
- 可以控制类的数量,不至于造成很多类
- 在增加或删除调料种类时,代码的维护量很大
- 考虑到用户可以添加多份调料时,可以将hasMilk返回一个对应int
- 考虑使用装饰者模式
四、装饰者模式
1.装饰者模式定义
- 装饰者模式:动态的将新功能附加到新对象上,在对象功能扩展方面,它比继承更有弹性,装饰者模式也体现了开闭原则
2.装饰者模式原理
- 装饰者模式就像打包一个快递 主体:比如:陶瓷,衣服(Component) 被装饰者 包装:比如:报纸填充,塑料泡沫(Decorator)
- Commponent 主体:比如类似前面的Drink
- ConcreteComponent:具体的主体,比如前面的各个咖啡
- Decorator:装饰者,比如各个调料
- 在如图的Component与ConcreteComponent之间,如果ConcreteComponent之间,如果ConcreteComponent类还很多,还可以设计一个缓冲层,将共有的部分提取出来,抽象成一个类
五、装饰者模式解决星巴克咖啡订单
1.装饰者模式下的订单。2份巧克力+1份牛奶的LongBlack
2.代码
2.1主体 Component
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description :
*/
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;
}
/**
* 计算费用
* 子类来实现
* @return
*/
public abstract float cost();
}
2.2具体主体
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description :
*/
public class Coffee extends Drink {
@Override
public float cost() {
return super.getPrice();
}
}
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description :
*/
public class LongBlack extends Coffee {
public LongBlack() {
setDes("美式咖啡");
setPrice(5);
}
}
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description :
*/
public class ShortBlack extends Coffee {
public ShortBlack() {
setDes("shortBlack");
setPrice(4.0f);
}
}
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description :
*/
public class DeCaf extends Coffee {
public DeCaf() {
setDes("无印咖啡");
setPrice(1.0f);
}
}
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description :
*/
public class Espresso extends Coffee {
public Espresso() {
setDes("意大利咖啡");
setPrice(6);
}
}
2.3装饰者
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description : 装饰着
*/
public class Decorator extends Drink{
private Drink obj;
@Override
public float cost() {
//getPrice 拿到自己的价格
return super.getPrice() + obj.cost();
}
public Decorator(Drink obj) {
this.obj = obj;
}
@Override
public String getDes() {
// obj.geetDes 输出被装饰着的信息
return super.getDes() + "" +super.getPrice() + "&&" + obj.getDes();
}
}
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description :
*/
public class Milk extends Decorator {
public Milk(Drink obj) {
super(obj);
setDes("牛奶");
setPrice(2.0f);
}
}
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description :
*/
public class Soy extends Decorator {
public Soy(Drink obj) {
super(obj);
setDes("豆浆");
setPrice(1.5f );
}
}
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description : 具体的装饰者
*/
public class Chocolate extends Decorator {
public Chocolate(Drink obj) {
super(obj);
setDes("巧克力");
//当前调味品的价格
setPrice(3.0f);
}
}
2.4装饰者下订单
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/11
* @description :
*/
public class CoffeBar {
public static void main(String[] args) {
//装饰着下订单
//1 点一分 LongBlack
Drink order = new LongBlack();
System.out.println("费用=1" + order.getPrice());
System.out.println("描述=" + order.getDes());
//2 加入一份牛奶
order = new Milk(order);
System.out.println("加入牛奶后费用=1" + order.getPrice());
System.out.println("加入牛奶后描述=" + order.getDes());
//3加入一份巧克力
order = new Chocolate(order);
System.out.println("加入牛奶又加入巧克力后费用=1" + order.getPrice());
System.out.println("加入牛奶后有加入巧克力描述=" + order.getDes());
//3加入一份巧克力
order = new Chocolate(order);
System.out.println("加入牛奶又加入巧克力后费用=1" + order.getPrice());
System.out.println("加入牛奶后有加入巧克力描述=" + order.getDes());
//无印咖啡
System.out.println("====================");
Drink order2 = new DeCaf();
System.out.println("费用=2" + order2.getPrice());
System.out.println("描述=" + order2.getDes());
order2 = new Milk(order2);
System.out.println("加入牛奶后费用=1" + order2.getPrice());
System.out.println("加入牛奶后描述=" + order2.getDes());
}
}
六、装饰者模式在jdk中的源码分析