qidao123.com技术社区-IT企服评测·应用市场

标题: 【设计模式】告别继承噩梦:用装饰者模式简化代码结构 [打印本页]

作者: 罪恶克星    时间: 2025-3-11 11:48
标题: 【设计模式】告别继承噩梦:用装饰者模式简化代码结构
概述

我们先来看一个快餐店的例子。
快餐店有炒面、炒饭这些快餐,可以额外附加鸡蛋、火腿、培根这些配菜,固然加配菜需要额外加钱,每个配菜的价钱通常不太一样,那么计算总价就会显得比较麻烦。

利用继承的方式存在的问题:
定义:指在不改变现有对象结构的环境下,动态地给该对象增加一些职责(即增加其额外功能)的模式。
结构

装饰(Decorator)模式中的角色:
案例

我们利用装饰者模式对快餐店案例进行改进,体会装饰者模式的精华。
类图如下:

代码如下:
  1. //快餐接口
  2. public abstract class FastFood {
  3.     private float price;
  4.     private String desc;
  5.     public FastFood() {
  6.     }
  7.     public FastFood(float price, String desc) {
  8.         this.price = price;
  9.         this.desc = desc;
  10.     }
  11.     public void setPrice(float price) {
  12.         this.price = price;
  13.     }
  14.     public float getPrice() {
  15.         return price;
  16.     }
  17.     public String getDesc() {
  18.         return desc;
  19.     }
  20.     public void setDesc(String desc) {
  21.         this.desc = desc;
  22.     }
  23.     public abstract float cost();  //获取价格
  24. }
  25. //炒饭
  26. public class FriedRice extends FastFood {
  27.     public FriedRice() {
  28.         super(10, "炒饭");
  29.     }
  30.     public float cost() {
  31.         return getPrice();
  32.     }
  33. }
  34. //炒面
  35. public class FriedNoodles extends FastFood {
  36.     public FriedNoodles() {
  37.         super(12, "炒面");
  38.     }
  39.     public float cost() {
  40.         return getPrice();
  41.     }
  42. }
  43. //配料类
  44. public abstract class Garnish extends FastFood {
  45.     private FastFood fastFood;
  46.     public FastFood getFastFood() {
  47.         return fastFood;
  48.     }
  49.     public void setFastFood(FastFood fastFood) {
  50.         this.fastFood = fastFood;
  51.     }
  52.     public Garnish(FastFood fastFood, float price, String desc) {
  53.         super(price,desc);
  54.         this.fastFood = fastFood;
  55.     }
  56. }
  57. //鸡蛋配料
  58. public class Egg extends Garnish {
  59.     public Egg(FastFood fastFood) {
  60.         super(fastFood,1,"鸡蛋");
  61.     }
  62.     public float cost() {
  63.         return getPrice() + getFastFood().getPrice();
  64.     }
  65.     @Override
  66.     public String getDesc() {
  67.         return super.getDesc() + getFastFood().getDesc();
  68.     }
  69. }
  70. //培根配料
  71. public class Bacon extends Garnish {
  72.     public Bacon(FastFood fastFood) {
  73.         super(fastFood,2,"培根");
  74.     }
  75.     @Override
  76.     public float cost() {
  77.         return getPrice() + getFastFood().getPrice();
  78.     }
  79.     @Override
  80.     public String getDesc() {
  81.         return super.getDesc() + getFastFood().getDesc();
  82.     }
  83. }
  84. //测试类
  85. public class Client {
  86.     public static void main(String[] args) {
  87.         //点一份炒饭
  88.         FastFood food = new FriedRice();
  89.         //花费的价格
  90.         System.out.println(food.getDesc() + " " + food.cost() + "元");
  91.         System.out.println("========");
  92.         //点一份加鸡蛋的炒饭
  93.         FastFood food1 = new FriedRice();
  94.         food1 = new Egg(food1);
  95.         //花费的价格
  96.         System.out.println(food1.getDesc() + " " + food1.cost() + "元");
  97.         System.out.println("========");
  98.         //点一份加培根的炒面
  99.         FastFood food2 = new FriedNoodles();
  100.         food2 = new Bacon(food2);
  101.         //花费的价格
  102.         System.out.println(food2.getDesc() + " " + food2.cost() + "元");
  103.     }
  104. }
复制代码
好处:
利用场景

源码解析 - IO流包装类

IO流中的包装类利用到了装饰者模式。BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter。
我们以BufferedWriter举例来阐明,先看看如何利用BufferedWriter
  1. public class Demo {
  2.     public static void main(String[] args) throws Exception{
  3.         //创建BufferedWriter对象
  4.         //创建FileWriter对象
  5.         FileWriter fw = new FileWriter("C:\\Users\\Think\\Desktop\\a.txt");
  6.         BufferedWriter bw = new BufferedWriter(fw);
  7.         //写数据
  8.         bw.write("hello Buffered");
  9.         bw.close();
  10.     }
  11. }
复制代码
利用起来感觉确实像是装饰者模式,接下来看它们的结构:

小结:BufferedWriter利用装饰者模式对Writer子实现类进行了增强,添加了缓冲区,提高了写数据的效率。
代理和装饰者的区别

静态代理和装饰者模式的区别:
往期推荐


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4