您的位置:首页 > 编程语言 > Java开发

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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: