装饰者模式
来源:互联网 发布:java打字小游戏源代码 编辑:程序博客网 时间:2024/06/07 00:24
package C_Decker;
/**
* 设计原则:类应该对扩展开放,对修改关闭。
* 装饰者模式:动态地将责任和行为附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
*
* 装饰者模式特点:
* 1:装饰者和被装饰对象有相同的超类型。
* 2:可以用一个或多个装饰者包装一个对象。
* 3:既然装饰者和被装饰对象有相同的超类型,所以在任何需要原始对象(被包装的)的场合,
* 4:可以用装饰过的对象代替它。
* 5:装饰者可以在所委托被装饰者的行为之前与/或之后,加上自己的行为,以达到特定的目的。
* 6:对象可以在任何时候被装饰,所以可以在运行时动态地、不限量地用你喜欢的装饰者来装饰对象。
* 7:装饰者会导致设计中出现许多小对象,如果过度使用,会让程序变得很复杂。
*
* 在这里使用继承达到”类型匹配”而不是继承”行为”
*/
public class Main {
public static void main(String[] args) {
// 订一杯espresso不需要调料
Espresso espresso = new Espresso();
System.out.println(espresso.getDeScription() + ” ” + espresso.cost());
Beverage beverage1 = new HouseBlend(); beverage1 = new Mocha(beverage1); beverage1 = new Mocha(beverage1); beverage1 = new Whip(beverage1); System.out.println(beverage1.getDeScription() + " " + beverage1.cost());}
}
package C_Decker;
/* 饮料类 /
public abstract class Beverage {
public static final int TALL = 10;
public static final int GRANDE = 20;
public static final int VENTI = 25;
String deScription = “未知饮料”;
int size;
public void setSize(int size) { this.size = size;}public int getSize() { return size == 0 ? TALL : size;}public String getDeScription() { return deScription;}abstract double cost();
}
/* 调料装饰类 /
abstract class CondimentDecorator extends Beverage {
public abstract String getDeScription();
}
/* 饮料代码 /
class Espresso extends Beverage {
public Espresso() { deScription = "Espersso";}double cost() { return 1.99;}
}
class HouseBlend extends Beverage {
public HouseBlend() {
deScription = “HouseBlend”;
}
double cost() { return 1.99;}
}
/* 调料代码 /
class Mocha extends CondimentDecorator {
Beverage beverage;
public Mocha(Beverage beverage) { this.beverage = beverage;}public String getDeScription() { // 利用委托,得到饮料叙述,然后在后面附加叙述Mocha return beverage.getDeScription() + ",Mocha";}double cost() { return 20 + beverage.cost();}
}
class Whip extends CondimentDecorator {
Beverage beverage;
public Whip(Beverage beverage) { this.beverage = beverage;}public String getDeScription() { // 利用委托,得到饮料叙述,然后在后面附加叙述Mocha return beverage.getDeScription() + ",Whip";}double cost() { return 20 + beverage.cost();}
}
// 根据大小收取相应的费用
class Soy extends CondimentDecorator {
Beverage beverage;
public Soy(Beverage beverage) { this.beverage = beverage;}public int getSize() { return beverage.getSize();}public String getDeScription() { return beverage.getDeScription() + ",Soy";}double cost() { double cost = beverage.cost(); if (getSize() == Beverage.TALL) { cost += .10; } else if (getSize() == Beverage.GRANDE) { cost += .15; } else if (getSize() == Beverage.VENTI) { cost += .20; } return cost;}
}
- 装饰者模式(Derector)
- 装饰者模式
- Decorator 装饰者模式
- 装饰者设计模式
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式(Decorator)
- 装饰者模式
- 装饰者模式
- 装饰者模式
- 装饰者模式 DecoratorPattern
- 设计模式 - 装饰者
- 装饰者模式
- 装饰者模式
- 装饰者模式 - 2
- 装饰者模式
- 装饰者模式
- git lab 打 Tag
- linux的rootfs解析和挂载
- 数字图像学习邻域运算
- Spring Session Redis集成(一)
- 谈谈程序猿的职业方向
- 装饰者模式
- 判断两个时间段是否重叠的算法
- Boost 常用的编译命令
- nodejs学习笔记5
- 获取当前机器的系统指标
- 新年新目标
- MySQL数据库的备份与恢复
- 使用拦截器Filter修补输入框的xss漏洞
- 数字图像学习 形态学运算