美文网首页
设计模式:装饰者模式

设计模式:装饰者模式

作者: 谁家的猪 | 来源:发表于2019-08-02 08:14 被阅读0次

定义与类型

  • 定义:在不改变原有对象的基础之上,将功能附加到对象上
  • 提供了比继承更有弹性的替代方案(扩展原有对象功能)
  • 类型:结构型

适用场景

  • 扩展一个类的功能或给一个类添加附加职责
  • 动态的给一个对象添加功能,这些功能可以再动态的撤销

优点

  • 继承的有力补充,比继承灵活,不改变原有对象的情况下给一个对象扩展功能
  • 通过使用不同的装饰类以及这些装饰类的排列组合,可以实现不同效果
  • 符合开闭原则

缺点

  • 会出现更多的代码,更多的类,增加程序复杂性
  • 动态装饰时,多层装饰时会更复杂

相关设计模式

  • 装饰者模式和代理模式
  • 装饰者模式和适配器模式

代码演示

卖煎饼问题,可以加鸡蛋,可以加香肠

  1. 创建Battercake类
/**
 * 煎饼
 * @author lijiayin
 */
public class Battercake {
    public String getDesc(){
        return "煎饼";
    }
    public int cost(){
        return 8;
    }
}
  1. 创建BattercakeWithEgg类
/**
 * 加蛋的煎饼
 * @author lijiayin
 */
public class BattercakeWithEgg extends Battercake{
    @Override
    public String getDesc() {
        return super.getDesc() + " 加一个蛋";
    }

    @Override
    public int cost() {
        return super.cost() + 1;
    }
}
  1. 创建BattercakeWithEggSausage类
/**
 * 加蛋、加香肠的煎饼
 * @author lijiayin
 */
public class BattercakeWithEggSausage extends BattercakeWithEgg{
    @Override
    public String getDesc() {
        return super.getDesc() + " 加一根香肠";
    }

    @Override
    public int cost() {
        return super.cost() + 2;
    }
}
  1. 测试一下
/**
 * 加蛋、加香肠的煎饼
 * @author lijiayin
 */
public class BattercakeWithEggSausage extends BattercakeWithEgg{
    @Override
    public String getDesc() {
        return super.getDesc() + " 加一根香肠";
    }

    @Override
    public int cost() {
        return super.cost() + 2;
    }
}
  1. 结果


    测试结果.png

加两个鸡蛋怎么办

  1. 创建ABattercake类
/**
 * @author lijiayin
 */
public abstract class ABattercake {
    public abstract String getDesc();
    public abstract int cost();
}
  1. 创建AbstractDecorator类
/**
 * @author lijiayin
 */
public class AbstractDecorator extends ABattercake {
    
    private ABattercake aBattercake;
    
    public AbstractDecorator(ABattercake aBattercake){
        this.aBattercake = aBattercake;
    }
    
    @Override
    public String getDesc() {
        return aBattercake.getDesc();
    }

    @Override
    public int cost() {
        return aBattercake.cost();
    }
}
  1. 创建Battercake类
/**
 * @author lijiayin
 */
public class Battercake extends ABattercake{

    @Override
    public String getDesc() {
        return "煎饼";
    }

    @Override
    public int cost() {
        return 8;
    }
}
  1. 创建EggDecorator类
/**
 * @author lijiayin
 */
public class EggDecorator extends AbstractDecorator {
    public EggDecorator(ABattercake aBattercake) {
        super(aBattercake);
    }

    @Override
    public String getDesc() {
        return super.getDesc() + " 加一个蛋";
    }

    @Override
    public int cost() {
        return super.cost() + 1;
    }
}
  1. 创建SausageDecorator类
/**
 * @author lijiayin
 */
public class SausageDecorator extends AbstractDecorator {
    public SausageDecorator(ABattercake aBattercake) {
        super(aBattercake);
    }

    @Override
    public String getDesc() {
        return super.getDesc() + " 加一根香肠";
    }

    @Override
    public int cost() {
        return super.cost() + 2;
    }
}
  1. 测试一下
/**
 * @author lijiayin
 */
public class Test {
    public static void main(String[] args) {
        ABattercake aBattercake = new Battercake();
        aBattercake = new EggDecorator(aBattercake);
        aBattercake = new EggDecorator(aBattercake);
        aBattercake = new SausageDecorator(aBattercake);
        System.out.println(aBattercake.getDesc() + ",价格:" + aBattercake.cost());
    }
}
  1. 测试结果


    测试结果.png
  2. UML图


    UML.png

框架源码

  1. jdk中的IO流
  2. spring的TransactionAwareCacheDecorator
  3. tomcat的HttpServletRequestWrapper

相关文章

  • 设计模式

    设计模式 单例模式、装饰者模式、

  • 设计模式笔记汇总

    目录 设计原则 “依赖倒置”原则 未完待续... 设计模式 设计模式——策略模式 设计模式——装饰者模式 设计模式...

  • java IO 的知识总结

    装饰者模式 因为java的IO是基于装饰者模式设计的,所以要了解掌握IO 必须要先清楚什么事装饰者模式(装饰者模式...

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

  • JavaScript 设计模式核⼼原理与应⽤实践 之 结构型设计

    JavaScript 设计模式核⼼原理与应⽤实践 之 结构型设计模式 装饰器模式,又名装饰者模式。它的定义是“在不...

  • 8种设计模式:

    主要介绍 单例设计模式,代理设计模式,观察者设计模式,模板模式(Template), 适配器模式,装饰模式(Dec...

  • 装饰者模式

    JavaScript 设计模式 张容铭第十二章 房子装修--装饰者模式 (102页) 装饰者模式(Decorato...

  • Summary of February 2017

    READING Head First 设计模式:完成50%。内容:观察者模式、装饰者模式、工厂模式、单件模式、命令...

  • 装饰对象:装饰者模式

    装饰对象:装饰者模式   这是《Head First设计模式(中文版)》第三章的读书笔记。   装饰者模式,可以称...

  • 设计模式之装饰器模式

    也称装饰者模式、装饰器模式、Wrapper、Decorator。 装饰模式是一种结构型设计模式,允许你通过将对象放...

网友评论

      本文标题:设计模式:装饰者模式

      本文链接:https://www.haomeiwen.com/subject/lomfrctx.html