Java设计模式之装饰模式

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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值