【设计模式sgg】设计模式----—9装饰者模式
2023-09-11 14:16:58 时间
Drink
package com.xinny._9decorator;
/**
* @author ChristinaXinny
* @create 2021-04-18 7:53 下午
*/
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
package com.xinny._9decorator;
/**
* @author ChristinaXinny
* @create 2021-04-18 7:57 下午
*/
public class Coffee extends Drink{
@Override
public float cost() {
return super.getPrice();
}
}
LongBlackCoffee
package com.xinny._9decorator;
/**
* @author ChristinaXinny
* @create 2021-04-18 7:59 下午
*/
public class LongBlackCoffee extends Coffee {
public LongBlackCoffee() {
setDes(" 美式咖啡 ");
setPrice(5.0f);
}
}
ShortBlackCoffee
package com.xinny._9decorator;
/**
* @author ChristinaXinny
* @create 2021-04-18 8:01 下午
*/
public class ShortBlackCoffee extends Coffee{
public ShortBlackCoffee() {
setDes(" 浓缩咖啡 ");
setPrice(4.0f);
}
}
EspressoCoffee
package com.xinny._9decorator;
/**
* @author ChristinaXinny
* @create 2021-04-18 7:58 下午
*/
public class EspressoCoffee extends Coffee{
public EspressoCoffee() {
setDes(" 意大利咖啡 ");
setPrice(6.0f);
}
}
Decorator
package com.xinny._9decorator;
/**
* @author ChristinaXinny
* @create 2021-04-18 8:02 下午
*/
// 这个是调味品
public class Decorator extends Drink{
private Drink drink;
//聚合
public Decorator(Drink drink) {
this.drink = drink;
}
@Override
public float cost() {
//super.price 是这个调料的价格
//drink.price 是饮料的价格 比如说浓缩
return super.getPrice() + drink.cost();
}
@Override
public String getDes() {
// drink.getDes() 这个是饮料的信息
// super.getDes() + " " + super.getPrice() :这里的super都是配料的信息
return super.getDes() + " " + super.getPrice() + " &&" + drink.getDes() + drink.getPrice() + " " ;
}
}
ChocolateDecorator
package com.xinny._9decorator;
/**
* @author ChristinaXinny
* @create 2021-04-18 8:08 下午
*/
//调味品 : 巧克力
public class ChocolateDecorator extends Decorator {
public ChocolateDecorator(Drink drink) {
super(drink);
setDes(" 巧克力 ");
setPrice(3.0f); //当前的调味品的价格
}
}
MilkDecorator
package com.xinny._9decorator;
/**
* @author ChristinaXinny
* @create 2021-04-18 8:11 下午
*/
//调味品 : 牛奶
public class MilkDecorator extends Decorator {
public MilkDecorator(Drink drink) {
super(drink);
setDes(" milk ");
setPrice(2.0f);
}
}
SoyDecorator
package com.xinny._9decorator;
/**
* @author ChristinaXinny
* @create 2021-04-18 8:13 下午
*/
//调味品 :豆浆
public class SoyDecorator extends Decorator{
public SoyDecorator(Drink drink) {
super(drink);
setDes(" 豆浆 ");
setPrice(1.0f);
}
}
ClientCoffeeBar
package com.xinny._9decorator;
/**
* @author ChristinaXinny
* @create 2021-04-18 8:16 下午
*/
// 咖啡店
public class ClientCoffeeBar {
public static void main(String[] args) {
// 装饰者模式下的订单 : 2*chocolate + 1*milk + 1*LongBlackCoffee
// 1。 先点一份longBlackCoffee
Drink order = new LongBlackCoffee();
System.out.println("订单1 cost: "+order.cost());
System.out.println("描述: " + order.getDes());
// 2。 再加一份牛奶
order = new MilkDecorator(order);
System.out.println("order 加入一份牛奶后的价格: " + order.cost() );
System.out.println("order 现在的描述: " + order.getDes() );
// 3。 再加一份巧克力
order = new ChocolateDecorator(order);
System.out.println("order 加入一份巧克力后的价格: " + order.cost() );
System.out.println("order 现在的描述: " + order.getDes() );
// 4。 再加一份巧克力
order = new ChocolateDecorator(order);
System.out.println("order 加入一份巧克力后的价格: " + order.cost() );
System.out.println("order 现在的描述: " + order.getDes() );
/*
订单1 cost: 5.0
描述: 美式咖啡
order 加入一份牛奶后的价格: 7.0
order 现在的描述: milk 2.0 && 美式咖啡 5.0
order 加入一份巧克力后的价格: 10.0
order 现在的描述: 巧克力 3.0 && milk 2.0 && 美式咖啡 5.0 2.0
order 加入一份巧克力后的价格: 13.0
order 现在的描述: 巧克力 3.0 && 巧克力 3.0 && milk 2.0 && 美式咖啡 5.0 2.0 3.0
【关于后面的5.0 2.0 3.0 是因为在decorator类里面
@Override
public String getDes() {
// drink.getDes() 这个是饮料的信息
// super.getDes() + " " + super.getPrice() :这里的super都是配料的信息
return super.getDes() + " " + super.getPrice() + " &&" + drink.getDes() + drink.getPrice() + " " ;
}
在最后调用了drink.getPrice() 而这个方法 会去调用每一层的包裹的getPrice
先去最内层去调用getPrice --- 就是美式咖啡
在到第二层的milk
再是第三层的chocolate
为什么没有最后一次的chocolate
因为最后一个在前面就调用了 也就是super.getDes()
所以super才是最后一层的chocolate
Process finished with exit code 0
*/
}
}
关于最后的5.0 2.0 3.0 ClientCoffeeBar里面有说
看看java的源码
利用了装饰着模式的 比如io流
相关文章
- 设计模式(七)学习----命令模式
- 设计模式(六)学习----组合模式
- 代理者模式----设计模式系列
- 设计模式(二)—— 简单工厂模式
- 炒冷饭系列:设计模式 建造者模式
- Java设计模式----观察者模式详解
- 【设计模式】业务代表模式
- 不一样的职责链模式(设计模式一)
- C++设计模式12--建造者模式----创建复杂对象的工程师
- C++设计模式---享元模式----浪费可耻,节俭光荣
- 「补课」进行时:设计模式(2)——通过一个超级汽车工厂来了解工厂模式
- 设计模式--原型模式
- 不一样的代理模式(设计模式十三)
- 23种设计模式介绍(三)---- 行为型模式
- 23种设计模式介绍(二)---- 结构型模式
- C++设计模式之组合模式的简单学习
- 设计模式学习总结(四)——代理模式
- 设计模式:策略模式
- 设计模式(Python语言)----代理模式
- 设计模式(Python语言)----适配器模式
- 设计模式(Python语言)----单例模式
- 设计模式(Python语言)----简单工厂模式
- 设计模式----责任链模式
- Java 设计模式——组合模式
- 【设计模式】原型模式