• 设计模式装饰器模式


    装饰器模式

    • 定义:在不改变先有对象结构下,动态地给该对象增加一些职责的模式;

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

      • 抽象构件(Component)角色 :定义一个抽象接口以规范准备接收附加责任的对象。
      • 具体构件(Concrete Component)角色 :实现抽象构件,通过装饰角色为其添加一些职责。
      • 抽象装饰(Decorator)角色 : 继承或实现抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。
      • 具体装饰(ConcreteDecorator)角色 :实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。
    • 我们使用装饰者模式对快餐店案例进行改进,体会装饰者模式的精髓。
      示例:

    • 类图:

    • 先定义一个抽象类FastFood

    package com.designparttern.Decorator;
    
    /**
     * @author tian.jiyun
     * @date 2022/4/28 22:29
     */
    public abstract class FastFood {
    
        private float price;
        private String desc;
    
        public FastFood() {
        }
    
        public FastFood(float price, String desc) {
            this.price = price;
            this.desc = desc;
        }
    
        public float getPrice() {
            return price;
        }
    
        public void setPrice(float price) {
            this.price = price;
        }
    
        public String getDesc() {
            return desc;
        }
    
        public void setDesc(String desc) {
            this.desc = desc;
        }
    
        public abstract float cost();//价格
    }
    
    • 继承抽象类的具体类1
    package com.designparttern.Decorator;
    
    /**
     * @author tian.jiyun
     * @date 2022/4/28 22:31
     */
    public class FriedRice extends FastFood{
        public FriedRice() {
            super(10, "炒饭");
        }
    
        @Override
        public float cost() {
            return getPrice();
        }
    }
    
    • 继承抽象类的具体类2
    package com.designparttern.Decorator;
    
    /**
     * @author tian.jiyun
     * @date 2022/4/28 22:32
     */
    public class FriedNoodles extends FastFood{
        public FriedNoodles() {
            super(13, "炒面");
        }
    
        @Override
        public float cost() {
            return getPrice();
        }
    }
    

    抽象装饰器类:

    package com.designparttern.Decorator;
    
    /**
     * @author tian.jiyun
     * @date 2022/4/28 22:33
     */
    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;
        }
    }
    
    • 具体装饰器类1:
    package com.designparttern.Decorator;
    
    /**
     * @author tian.jiyun
     * @date 2022/4/28 22:36
     */
    public class Egg extends Garnish{
        public Egg(FastFood fastfood, float price, String desc) {
            super(fastfood, price, desc);
        }
    
        @Override
        public float cost() {
            return getPrice() + getFastFood().cost();
        }
    
        @Override
        public String getDesc() {
            return super.getDesc() + getFastFood().getDesc();
        }
    }
    
    • 具体装饰器类2:
    package com.designparttern.Decorator;
    
    /**
     * @author tian.jiyun
     * @date 2022/4/28 22:36
     */
    public class Bacon extends Garnish{
        public Bacon(FastFood fastfood, float price, String desc) {
            super(fastfood, price, desc);
        }
    
        @Override
        public float cost() {
            return getPrice() + getFastFood().cost();
        }
    
        @Override
        public String getDesc() {
            return super.getDesc() + getFastFood().getDesc();
        }
    }
    

    测试类:

    package com.designparttern.Decorator;
    
    /**
    * @author tian.jiyun
    * @date 2022/4/28 22:39
    */
    public class Client {
       public static void main(String[] args) {
           FastFood fastFood = new FriedNoodles();
           System.out.println(fastFood.getDesc() + " " + fastFood.cost());
           System.out.println("========================================");
    
           FastFood food1 = new Egg(fastFood, 3, "鸡蛋");
           System.out.println(food1.getDesc() + " " + food1.cost());
           System.out.println("========================================");
    
           FastFood food2 = new Bacon(food1,5,"培根");
           System.out.println(food2.getDesc() + " " + food2.cost());
           System.out.println("========================================");
    
           FastFood food3 = new Bacon(fastFood,5,"培根");
           System.out.println(food3.getDesc() + " " + food3.cost());
           System.out.println("========================================");
       }
    }
    
    

    运行结果:

    好处:

    • 饰者模式可以带来比继承更加灵活性的扩展功能,使用更加方便,可以通过组合不同的装饰者对象来获取具有不同行为状态的多样化的结果。装饰者模式比继承更具良好的扩展性,完美的遵循开闭原则,继承是静态的附加责任,装饰者则是动态的附加责任。

    • 装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。

    扩展:
    装饰器模式和静态代理模式区别:

    • 相同点:
      • 都要实现与目标类相同的业务接口
      • 在两个类中都要声明目标对象
      • 都可以在不修改目标类的前提下增强目标方法
    • 不同点:
      • 目的不同
        装饰者是为了增强目标对象
        静态代理是为了保护和隐藏目标对象
      • 获取目标对象构建的地方不同
        装饰者是由外界传递进来,可以通过构造方法传递
        静态代理是在代理类内部创建,以此来隐藏目标对象

    如需要看代理模式,请至连接:https://www.cnblogs.com/skyfreedom/p/16194681.html

  • 相关阅读:
    FreeBSD使用多线程下载工具axel
    类UNIX系统基础:文件安全与权限
    基于pf防火墙控制IP连接数
    在FreeBSD下搭建高性能企业级网关与代理服务器
    搭建自己的CVSup服务器
    转:Spring技术内幕——深入解析Spring架构与设计原理(三)IOC实现原理
    Spring Web MVC的实现(二)
    java中HashSet详解
    转:Spring技术内幕——深入解析Spring架构与设计原理(二)IOC实现原理
    DIV垂直水平都居中
  • 原文地址:https://www.cnblogs.com/skyfreedom/p/16208317.html
Copyright © 2020-2023  润新知