首页 > 其他 > 详细

设计模式 之 装饰器模式

时间:2016-08-16 00:37:15      阅读:228      评论:0      收藏:0      [点我收藏+]

我的理解,就是一种组合

package com.test.pattern.decorator;

//组件对象的接口,可以给这些对象动态地添加职责
abstract class Component {
    public abstract void operation();
}

//具体组件
class ConcreteComponent extends Component {

    @Override
    public void operation() {
        
    }
}

//装饰器接口,维持一个指向组件对象的接口对象, 并定义一个与组件接口一致的接口
abstract class Decorator extends Component {
    
    /** 持有组件对象 */
    protected Component component;
    
    public Decorator(Component component) {
        this.component = component;
    }
    
    public void operation() {
        component.operation();
    }
}

//装饰器的具体实现对象
class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) { 
        super(component);
    }    
    
    private void operationFirst() {}
    private void operationLast() {}
    public void operation() {
        operationFirst();
        super.operation();
        operationLast();
    }
}

class ConcreteDecoratorB extends Decorator {
    public ConcreteDecoratorB(Component component) { 
        super(component);
    }    
    
    private void operationFirst() {}
    private void operationLast() {}
    public void operation() {
        operationFirst();
        super.operation();
        operationLast();
    }
}

public class Client {
    public static void main(String[] args) {
        Component c1 = new ConcreteComponent();
        Decorator decoratorA = new ConcreteDecoratorA(c1);  //给对象透明的增加功能A并调用  
        decoratorA.operation();
        Decorator decoratorB = new ConcreteDecoratorB(c1); //给对象透明的增加功能B并调用
        decoratorB.operation();
        Decorator decoratorBandA = new ConcreteDecoratorB(decoratorA);//装饰器也可以装饰具体的装饰对象,此时相当于给对象在增加A的功能基础上在添加功能B  
        decoratorBandA.operation();  
    }
}

 

设计模式 之 装饰器模式

原文:http://www.cnblogs.com/heben/p/5774760.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!