Java设计模式之装饰模式
2018-02-03 14:38
441 查看
1、装饰模式
在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。装饰者与被装饰者拥实现了共同的接口。在装饰模式中的角色有:
● 抽象接口(Component)角色:规范准备接收附加责任的对象。
● 具体构件(ConcreteComponent)角色:定义一个将要接收附加责任的类。
● 装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
● 具体装饰(ConcreteDecorator)角色:负责给构件对象“贴上”附加的责任。
2、简单例子
(1)抽象接口/** * 被装饰类的接口 * @author hadron * */ public interface Component { void eat(); }
(2)具体的被装饰类
/** * 具体的被装饰类 * @author hadron * */ public class ConcreteComponent implements Component{ @Override public void eat() { System.out.println("先吃点水果"); } }
(3)装饰者基类
/** * 装饰者基类 * @author hadron */ public abstract class Decorator implements Component{ private Component component; public Decorator(Component component){ this.component = component; } @Override public void eat(){ component.eat(); } }
(4)一种装饰类
/** * 具体装饰类 * @author hadron * */ public class DecoratorA extends Decorator { public DecoratorA(Component action){ super(action); } @Override public void eat(){ super.eat(); System.out.println("来一碗牛蛙面"); } }
(5)另一种装饰类
public class DecoratorB extends Decorator { public DecoratorB(Component component) { super(component); } @Override public void eat(){ super.eat(); System.out.println("来一碗桂林米粉"); } }
(6)测试类
public class Main { public static void main(String[] args) { Component component=new ConcreteComponent(); //对component进行DecoratorA装饰 Decorator decoretor=new DecoratorA(component); decoretor.eat(); //对component进行DecoratorB装饰 decoretor=new DecoratorB(component); decoretor.eat(); } }
先吃点水果 来一碗牛蛙面 先吃点水果 来一碗桂林米粉
3、 Java IO中的装饰模式
在Java IO流处理中可以看到类似下面的语句InputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("FileTest.java")));
这是背后是装饰模式的实现。
InputStream中的装饰模式
抽象构件(Component)角色:由InputStream扮演。这是一个抽象类,为各种子类型提供统一的接口。
具体构件(ConcreteComponent)角色:由ByteArrayInputStream、FileInputStream、PipedInputStream、StringBufferInputStream等类扮演。它们实现了抽象构件角色所规定的接口。
抽象装饰(Decorator)角色:由FilterInputStream扮演。FilterInputStream实现了InputStream所规定的接口,子类有BufferedInputStream和DataInputStream。
具体装饰(ConcreteDecorator)角色:由几个类扮演,其中常用的有BufferedInputStream、DataInputStream。
(1) BufferedInputStream对InputStream进行了装饰,提高了输入效率,增加了输入缓冲区的功能
(2) DataInputSteam对InputStream进行了装饰,提供了许多特有方法,可以读输入流基本类型的操作,比如readInt(),readDouble(),readLong()
相关文章推荐
- javaseday21补充(装饰设计模式 BufferedReader 的小弟 LineNumber 行号媒体文件的复制)
- JAVA 设计模式之装饰模式
- java设计模式——装饰模式
- java二十三种设计模式------(一)装饰模式 装饰类VS子类继承
- Java设计模式-装饰器模式
- 【GOF23设计模式】_装饰模式_IO流底层架构_装饰和桥接模式的区别JAVA240
- java设计模式之结构型模式-装饰模式
- 黑马程序员——Java基础——IO流、字符流、字节流、缓冲区技术、装饰设计模式、LineNumberReader等
- java 策略设计模式 装饰器模式
- Java语言实现的装饰设计模式复习
- Java设计模式--装饰模式
- Java 设计模式情景分析——装饰模式
- java笔记--设计模式之装饰模式
- Java设计模式百例 - 装饰器模式
- JAVA设计模式讲解(原理+实例)---装饰模式
- JAVA设计模式:代理模式&& 装饰模式区别
- Java设计模式----装饰模式(Decorator)
- java设计模式---装饰模式
- (三)Java设计模式--装饰器模式
- java设计模式之装饰模式