装饰模式

装饰模式:即动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。(通常可以通过集成来实现功能的拓展,若需要增加的功能种类繁多,那么势必需要生成很多的子类,增加系统的复杂性,同时,使用继承实现功能的拓展,我们必须可预见这些拓展的功能,这些功能是编译时就确定的了,是静态的。)
下面为装饰模式的结构图:

这里写图片描述
其中:
Component:定义了一个对象接口,可以给这些对象动态地添加职责。
ConcreteComponent:定义了一个具体的对象,也可以给这个对象添加一些职责。
Decorator:装饰抽象类,继承了Component,从外类来扩展Component类的功能,但对于Component来说,是无需知道Decorator的存在的。
ConcreteDecorator:具体的封装对象,起到给Component添加职责的功能。
基本的代码实现如下:

//Component类
abstract class Component{
    public abstract void operation();
}
//ConcreteComponent类
class ConcreteComponent extends Component{
    public void operation(){
        System.out.println("具体对象的操作");
    }
}
//Decorator类
abstract class Decorator extends Component{
    protected Component component;

    public void setComponent(Component component) {
        this.component = component;
    }
    public void operation(){
        if(component!=null){
            component.operation();
        }
    }
}
//ConcreteDecoratorA
class ConcreteDecoratorA extends Decorator{
    //本类的独有功能,以区别于ConcreteDecoratoB
    private String addedState;
    public void operation(){
        //首先会运行原Component的operation(),再执行本类的功能,如addedState,相当于对原Component进行了装饰。
        super.operation();
        addedState="new state";
        System.out.println("具体装饰对象A的操作");
    }

}
//ConcreteDecoratorB
class ConcreteDecoratorB extends Decorator{
    public void operation(){
        //首先会运行原Component的operation(),再执行本类的功能,如addBehavior(),相当于对原Component进行了装饰。
        super.operation();
        addBehavior();
        System.out.println("具体装饰对象A的操作");
    }
    private void addBehavior(){

    }

}
//客户端代码
public class Client{
    public static void main(String [] args){
        /*
         * 装饰方法是:首先用ConcreteComponent实例化对象c,
         * 然后用ConcreteDecoratorA的实例化对象d1来包装c,
         * 再用ConcreteDecoratorB的实例化对象d2包装的d1,
         * 最终执行d2的operation().
         */
        ConcreteComponent c=new ConcreteComponent();
        ConcreteDecoratorA d1=new ConcreteDecoratorA();
        ConcreteDecoratorB d2=new ConcreteDecoratorB();
        d1.setComponent(c);
        d2.setComponent(d1);
        d2.operation();
    }
}

装饰者和被装饰者拥有共同的超类,Java的I/O API就是使用Decorator来实现的。下面举一个生活中的小例子:

//定义被装饰者类
interface Human{
    public void eatFood();
    public void sayWords();
}
//定义装饰者类
abstract class Decorator implements Human{
    private Human human;
    public Decorator(Human human){
        this.human=human;
    }
    public void eatFood(){
        human.eatFood();
    }
    public void sayWords(){
        human.sayWords();
    }
}
//下面为三种装饰,依次细化,就是装饰者功能越来越多。
class DecoratorFirst extends Decorator{
    public DecoratorFirst(Human human){
        super(human);
    }
    public void goDiner(){
        System.out.println("去饭店");
    }
    public void findDiner(){
        System.out.println("找到一家饭店");
    }   
    public void eatFood(){
        super.eatFood();
        goDiner();
    }
    public void sayWords(){
        super.sayWords();
        findDiner();
    }
}
class DecoratorSecond extends Decorator{
    public DecoratorSecond(Human human){
        super(human);
    }   
    public void goSeat(){
        System.out.println("找座位");
    }
    public void findSeat(){
        System.out.println("找到一个座位");
    }
    public void eatFood(){
        super.eatFood();
        goSeat();
    }
    public void sayWords(){
        super.sayWords();
        findSeat();
    }
}
class DecoratorThird extends Decorator{
    public DecoratorThird(Human human){
        super(human);
    }
    public void goMenu(){
        System.out.println("进行点餐"); 
    }
    public void findPizza(){
        System.out.println("要一份披萨");
    }
    public void eatFood(){
        super.eatFood();
        goMenu();
    }
    public void sayWords(){
        super.sayWords();
        findPizza();
    }
}
//定义被装饰者,被装饰者在初始状态有一些自己的装饰:
class Person implements Human{
    public void eatFood(){
        System.out.println("去吃东西");
    }
    public void sayWords(){
        System.out.println("我要说的是");
    }
}
//客户端类
public class Client{
    public static void main(String[] args){
        Human person=new Person();
        Decorator decorator=new DecoratorThird(new DecoratorSecond(new DecoratorFirst(person)));
        decorator.eatFood();
        decorator.sayWords();
    }
}
/*输出结果为:
 * 去吃东西:
 * 去饭店:
 * 找座位
 * 进行点餐
 * 我要说的是
 * 找到一家饭店
 * 找到一个座位
 * 要一份披萨
 * 
 */

装饰模式的优点:把类中的装饰功能从类中搬移出去,可以简化原有的类,有效地把类的核心职责和装饰功能区分开来,而且可以去除相关类中重复的装饰逻辑。

装饰模式总结:装饰模式是为已经有的功能动态添加更多功能的一种方法。当系统需要新功能的时候,是向旧的类中添加新的代码。这些新的代码通常装饰了原有类的核心职责或主要行为,但是这样做存在是我问题是:在主类中加入了新的字段,新的方法和新的逻辑,从而增加了主类的复杂度,且这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为的需要。而装饰模式对这样的问题提供了一个很好的解决方案:它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择的,按顺序的使用砖石功能包装对象了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值