装饰器设计模式应⽤-JDK源码⾥⾯的Stream IO流

装饰器设计模式在JDK源码⾥⾯应⽤场景

抽象组件(Component):InputStream

定义装饰⽅法的规范

被装饰者(ConcreteComponent) : FileInputStream、ByteArrayInputStream

Component的具体实现,也就是我们要装饰的具体对象

装饰者组件(Decorator):FilterInputStream

    定义具体装饰者的⾏为规范,Component⻆⾊有相同的接⼝,持有组件(Component)对象的实例引⽤
    ⾃⾏⻋组件 都有 名称和价格

具体装饰物(ConcreteDecorator):BufferedInputStream、
DataInputStream

    负责给构件对象装饰附加的功能
    ⽐如 喇叭,防爆胎

在这里插入图片描述

应⽤场景

//添加了Buffer缓冲功能
InputStream inputStream = new
BufferedInputStream(new FileInputStream(""));

猜你喜欢

转载自blog.csdn.net/csdn_mycsdn/article/details/131504153