java设计模式——装饰者模式

一. 定义与类型

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

类型:结构性

二. 使用场景

(1) 扩展一个类的功能或给一个类添加附加职责

(2) 动态的给一个对象添加功能,这些功能可以再动态的撤销

三. 优缺点

优点:

  (1) 是继承的有力补充,比继承灵活,不改变原有对象的情况下给一个对象扩展功能(其实是继承的基础上,进行提升)

  (2) 通过使用不同的装饰类已经这些装饰类的排列组合,可以实现不同效果

  (3) 符合开闭原则

缺点:

  (1) 会出现更多的代码,更多的类,增加程序复杂性

  (2) 动态装饰时,多层装饰时会更复杂

四. 相关设计模式

装饰者模式和代理模式

装饰者模式和适配器模式

五. Coding

 以一个具体的业务来进行coding,

具体的煎饼类:

/**
 * @program: designModel
 * @description: 煎饼类
 * @author: YuKai Fan
 * @create: 2019-02-11 14:03
 **/
public class Battercake {

    protected String getDesc() {
        return "煎饼";
    }

    protected int cost() {
        return 8;
    }
}
/**
 * @program: designModel
 * @description:在煎饼上加鸡蛋
 * @author: YuKai Fan
 * @create: 2019-02-11 14:04
 **/
public class BattercakeWithEgg extends Battercake {
    @Override
    public String getDesc() {
        return super.getDesc() + " 加一个鸡蛋";
    }

    @Override
    public int cost() {
        return super.cost() + 1;
    }
}
/**
 * @program: designModel
 * @description:在鸡蛋煎饼上加香肠
 * @author: YuKai Fan
 * @create: 2019-02-11 14:08
 **/
public class BattercakeWithEggSausage extends BattercakeWithEgg {
    @Override
    public String getDesc() {
        return super.getDesc() + " 加一根香肠";
    }

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

应用层:

/**
 * @program: designModel
 * @description:
 * @author: YuKai Fan
 * @create: 2019-02-11 14:08
 **/
public class Test {
    public static void main(String[] args) {
        Battercake battercake = new Battercake();
        System.out.println(battercake.getDesc() + " 销售价格:" + battercake.cost());

        BattercakeWithEgg battercakeWithEgg = new BattercakeWithEgg();
        System.out.println(battercakeWithEgg.getDesc() + " 销售价格:" + battercakeWithEgg.cost());

        BattercakeWithEggSausage battercakeWithEggSausage = new BattercakeWithEggSausage();
        System.out.println(battercakeWithEggSausage.getDesc() + " 销售价格:" + battercakeWithEggSausage.cost());
    }
}

结果:

java设计模式——装饰者模式

UML类图:

java设计模式——装饰者模式

上面这个例子,并不是装饰者模式,可以看最后的UML类图,每个层次之间都有联系。但是此时我想要两个鸡蛋,和一个香肠,上面那两个类,就无法在使用,需要在写一个类,并且再new出来

使用装饰者模式:

装饰者模式中要有抽象的实体类,具体的实体类,抽象的装饰者,具体的装饰者

还是以上面为例,其中食物或者未知的某个煎饼就是抽象的实体类;煎饼就是具体的实体类;用来装饰煎饼的东西就是抽象的装饰者;鸡蛋,香肠就是具体的装饰者

 创建一个抽象的煎饼类:

/**
 * @program: designModel
 * @description:
 * @author: YuKai Fan
 * @create: 2019-02-11 14:13
 **/
public abstract class ABattercake {
    protected abstract String getDesc();
    protected abstract int cost();
}

让煎饼来继承抽象的煎饼类:

/**
 * @program: designModel
 * @description:
 * @author: YuKai Fan
 * @create: 2019-02-11 14:14
 **/
public class Battercake extends ABattercake {
    protected String getDesc() {
        return "煎饼";
    }

    protected int cost() {
        return 8;
    }
}

在创建一个抽象的装饰者类,继承抽象的煎饼类,为什么要继承抽象的煎饼类?是因为要把装饰者与实体联系起来,它们都是ABattercake的子类,通过构造器将的抽象煎饼注入进去

/**
 * @program: designModel
 * @description: 这是抽象的装饰类,但是并没有被abstract修饰,
 *      这个类是否是抽象类,需要看业务场景。如果加上abstract作为抽象的类,能保证子类必须实现某个方法 doSomething()方法才会有意义
 *      举个例子,如果在加鸡蛋或者加香肠的时候都会有一个动作,而这个动作,分别用于各自的装饰者实现,那对于两个实体的装饰者的父类用抽象的装饰者,才会有意义。
 * @author: YuKai Fan
 * @create: 2019-02-11 14:15
 **/
public class AbstractDecorator extends ABattercake  {
    private ABattercake aBattercake;

    public AbstractDecorator(ABattercake aBattercake) {
        this.aBattercake = aBattercake;
    }

    //protected  abstract void doSomething();

    protected String getDesc() {
        return this.aBattercake.getDesc();
    }

    protected int cost() {
        return this.aBattercake.cost();
    }
}

具体的装饰者:鸡蛋,香肠

/**
 * @program: designModel
 * @description:
 * @author: YuKai Fan
 * @create: 2019-02-11 14:18
 **/
public class SausageDecorator extends AbstractDecorator {
    public SausageDecorator(ABattercake aBattercake) {
        super(aBattercake);
    }

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

    @Override
    protected int cost() {
        return super.cost() + 2;
    }
}
/**
 * @program: designModel
 * @description:
 * @author: YuKai Fan
 * @create: 2019-02-11 14:18
 **/
public class EggDecorator extends  AbstractDecorator {
    public EggDecorator(ABattercake aBattercake) {
        super(aBattercake);
    }

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

    @Override
    protected int cost() {
        return super.cost() + 1;
    }
}

应用层:

/**
 * @program: designModel
 * @description:
 * @author: YuKai Fan
 * @create: 2019-02-11 14:20
 **/
public class Test {
    public static void main(String[] args) {
        ABattercake aBattercake;
        aBattercake = new Battercake();
        aBattercake = new EggDecorator(aBattercake);
        aBattercake = new EggDecorator(aBattercake);
        aBattercake = new SausageDecorator(aBattercake);
        System.out.println(aBattercake.getDesc() + " 销售价格:" + aBattercake.cost());
    }
}

UML类图:

java设计模式——装饰者模式

结果:

java设计模式——装饰者模式

从上面的代码可以看出,装饰者模式其实是在继承的基础上,实现的一种设计模式思想

六. 源码解析

在jdk中这种模式体现最明显的就是I/O了,

BufferedReader,BufferedInputStream,BufferedOutputStream,FileInputStream,FilterInputStream

java设计模式——装饰者模式

在Spring中的TransactionAwareCacheDecorator

java设计模式——装饰者模式

在Servlet中的SessionRepositoryRequestWrapper

java设计模式——装饰者模式

java设计模式——装饰者模式

在mybaits中使用装饰者模式Cache接口

java设计模式——装饰者模式

从这个图可以看到,在 org.apache.ibatis.cacahe包下的decorators包中所有的类都是用来装饰Cache接口的,例如:FifoCache先进先出cache,LruCache最少使用cache,SoftCache软引用cache,TransactionalCache事务缓存