python 网站开发教程,微信小程序直播平台,wordpress incategory,图网文章目录 0、背景1、装饰者模式2、案例3、使用场景4、源码中的实际应用 0、背景
有个快餐店#xff0c;里面的快餐有炒饭FriedRice 和 炒面FriedNoodles#xff0c;且加配菜后总价不一样#xff0c;计算麻烦。如果单独使用继承#xff0c;那就是#xff1a; 类爆炸不说里面的快餐有炒饭FriedRice 和 炒面FriedNoodles且加配菜后总价不一样计算麻烦。如果单独使用继承那就是 类爆炸不说再来个炒河粉就发现这样写扩展性很差。
1、装饰者模式
指在不改变现有对象结构的情况下动态地给该对象增加一些职责即增加其额外功能的模式。角色
抽象构件具体构件的规范接口具体构件被装饰被增加功能的原始对象抽象装饰继承抽象构件具体装饰实现抽象装饰给具体构件对象添加功能
总之用于动态扩展一个类的功能增强目标对象而非使用继承
2、案例 定义快餐类抽象构件
//快餐接口
Getter
Setter
AllArgsConstructor
NoArgsConstructor
public abstract class FastFood {private float price;private String desc;public abstract float cost(); //获取价格
}定义炒饭、炒面类具体的构件
//炒饭
public class FriedRice extends FastFood {public FriedRice() {super(10, 炒饭); //调用父类的构造方法给炒饭的价格和描述赋值即一碗炒饭10元}public float cost() {return getPrice();}
}
//炒面
public class FriedNoodles extends FastFood {public FriedNoodles() {super(12, 炒面); //调用父类的构造方法给炒面的价格和描述赋值}public float cost() {return getPrice();}
}定义装饰类属于抽象装饰角色实现或者继承抽象构件并聚合它
//配料
public abstract class Garnish extends FastFood {private FastFood fastFood; //声明抽象构件的变量public FastFood getFastFood() {return fastFood;}public void setFastFood(FastFood fastFood) {this.fastFood fastFood;}public Garnish(FastFood fastFood, float price, String desc) {super(price,desc);this.fastFood fastFood;}
}定义配料类属于具体的装饰者角色继承装饰者类
//鸡蛋配料
public class Egg extends Garnish {public Egg(FastFood fastFood) { //给属性赋值super(fastFood,1,鸡蛋); //一个鸡蛋一块钱 一份快餐}Overridepublic float cost() {return getPrice() getFastFood().getPrice(); //鸡蛋的价格 快餐的价格}Overridepublic String getDesc() {return super.getDesc() getFastFood().getDesc();}
}
//培根配料
public class Bacon extends Garnish {public Bacon(FastFood fastFood) {super(fastFood,2,培根); //一个培根两块钱 一份快餐}Overridepublic float cost() {return getPrice() getFastFood().getPrice();}Overridepublic String getDesc() {return super.getDesc() getFastFood().getDesc();}
}测试
//测试类
public class Client {public static void main(String[] args) {//点一份炒饭FastFood food new FriedRice();//花费的价格System.out.println(food.getDesc() food.cost() 元);System.out.println();//点一份加鸡蛋的炒饭FastFood food1 new FriedRice();food1 new Egg(food1); 妙//花费的价格System.out.println(food1.getDesc() food1.cost() 元);System.out.println();//点一份加培根的炒面FastFood food2 new FriedNoodles();food2 new Bacon(food2); 妙//花费的价格System.out.println(food2.getDesc() food2.cost() 元);}
}后续如果需求变动要加一个新配料火腿那就定义一个类去继承Garnish类即可。且任何配料可以自由搭配任何主食组合不同的装饰者对象这比排列组合写出n个子类好多了。继承是静态的附加责任装饰者则是动态的附加责任。
3、使用场景
不能采用继承的方式对已有功能进行扩充时可用装饰者模式。比如
类被final修饰不能被继承扩展项目太多用继承会子类爆炸某些功能需要支持动态添加和动态撤销
4、源码中的实际应用
JDK中BufferedWriter等包装类用到了装饰者模式对Writer类进行了增强
public class Demo {public static void main(String[] args) throws Exception{//创建BufferedWriter对象//创建FileWriter对象FileWriter fw new FileWriter(C:\\Users\\Think\\Desktop\\a.txt);BufferedWriter bw new BufferedWriter(fw);//写数据bw.write(hello Buffered);bw.close();}
}