设计模式之装饰者模式

前言:

相信Java开发者在使用java i/o API的时候,对于以下代码写法应该非常熟悉:

InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

在不知道具体设计实现之前,是否有人像我一样感觉这个读取文件信息的方法逻辑优点复杂。wtf,这是咋回事呢?顿时变成丈二的和尚,摸摸头。

所以,本篇的主题内容就出现了——装饰者模式。

介绍:

装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。使其在被装饰之后还具有原始的功能。

完全符合了对外开放,对内关闭的开闭原则。大多与工厂模式一起使用。

适用场景:

  1. 需要扩展一个类的功能,或给一个类添加附加职责。
  2. 需要动态的给一个对象添加功能,这些功能可以再动态的撤销。
  3. 需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。
  4. 当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
代码示例:
  • 抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
  • 具体构件(Concrete Component)角色:定义一个将要接收附加责任的类。
  • 装饰(Decorator)角色:持有一个构件(Component)对象的实例,并实现一个与抽象构件接口一致的接口。
  • 具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的责任。

一个制作冰淇淋的例子,冰淇淋可加奶油,巧克力:

抽象角色:IIceCream

public interface IIceCream {
    /**
     * 价格
     *
     * @return
     */
    double cost();

    /**
     * 详情描述
     *
     * @return
     */
    String getDesc();
}

装饰角色Decorator,实现IIceCream接口,使得在其装饰之后还具有原始功能:

public abstract class Decorator implements  IIceCream{
    protected IIceCream iceCream;

    Decorator(IIceCream iceCream){
        this.iceCream = iceCream;
    }

    public double cost() {
        return iceCream.cost();
    }

    public String getDesc() {
        return iceCream.getDesc();
    }
}

奶油装饰MilkDecorator:

public class MilkDecorator extends Decorator{
    MilkDecorator(IIceCream iceCream) {
        super(iceCream);
    }

    @Override
    public double cost() {
        //价格加2元
        return super.cost() + 2;
    }

    @Override
    public String getDesc() {
        return super.getDesc() + " added milk";
    }
}

巧克力装饰类ChocolatesDecorator:

public class ChocolatesDecorator extends Decorator {
    ChocolatesDecorator(IIceCream iceCream) {
        super(iceCream);
    }

    @Override
    public double cost() {
        //价格加4元
        return super.cost() + 4;
    }

    @Override
    public String getDesc() {
        return super.getDesc() + " added Chocolates";
    }
}

具体角色BigIceCream,大个冰淇淋,无任何添加元素:

public class BigIceCream implements IIceCream {
    public double cost() {
        return 5;
    }

    public String getDesc() {
        return "this is a big ice cream";
    }
}

测试用例:

  public static void main(String[] args) {
        IIceCream iceCream = new BigIceCream();
        iceCream = new MilkDecorator(iceCream);
        iceCream = new ChocolatesDecorator(iceCream);

        System.out.println("price:" + iceCream.cost() + " //  desc: " + iceCream.getDesc());
    }

运行结果如下:

price:11.0 //  desc: this is a big ice cream added milk added Chocolates

代码传送门:https://github.com/pffo/pattern/tree/master/src/main/java/com/ffo/pattern

如有不足之处,请多多指教。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值