结构型模式-装饰模式

一、装饰模式

1.    装饰模式(Decorator)的定义:又名包装(Wrapper)模式,装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。

                               2.    装饰模式以对客户端透明的方式动态的给一个对象附加上更多的责任。换言之客户端并不会觉的对象在装饰前和装饰后有什么区别。

                               3.    装饰模式可以在不创造更多的子类的模式下,将对象的功能加以扩展。

                               4.    装饰模式与类继承的区别:

                                      1)    装饰模式是一种动态行为,对已经存在类进行随意组合,而类的继承是一种静态的行为,一个类定义成什么样的,该类的对象便具有什么样的功能,

                                              无法动态的改变。

                                       2)    装饰模式扩展的是对象的功能,不需要增加类的数量,而类继承扩展是类的功能,在继承的关系中,如果我们想增加一个对象的功能,我们只能通过

                                               继承关系,在子类中增加两个方法。

                                       3)    装饰与继承比较图:

                                       4)    装饰模式是在不改变原类文件和使用继承的情况下,动态的扩展一个对象的功能,它是通过创建一个包装对象,也就是装饰来包裹真是的对象。

                              5.    装饰模式把对客户端的调用委派给被装饰的类,装饰模式的关键在于这种扩展完全透明的。

                              6.    装饰模式的构成:

                                      1)    抽象构建角色(Component):给出一个抽象的接口,以规范准备接受附加责任的对象。相当于i/o流里面InputStream/OutputStreamReader/Writer

                                      2)    具体的构建角色(ConcreteComponent):定义一个将要接受附加责任的类。相当于i/o里面的FileOutputStreamFileInputStream

                                      3)    装饰角色(Docorator):持有一个抽象构建(Component)角色的引用,并定义一个与抽象构件一致的接口。相当于i/o里面的FilerOutputStream

                                               FilterInputStream

                                      4)    具体的装饰角色(ConcreteDecorator):负责给构建对象“贴上”附加的责任。相当于i/o流里面的BufferedOutputStreamBufferedInputStream以及

                                              DataOutputStreamDataInputSrtream

                               7.    装饰模式的特点:

                                      1)    装饰对象和真实对象具有相同的接口,这样客户端对象就可以以真实对象的相同的方式和装饰对象交互。

                                       2)    装饰对象包含一个真实对象的引用(reference).

                                       3)    装饰对象接受所有来自客户端的请求,它把这些请求转发给真实的对象。

                                       4)    装饰对象可以在转发这些请求以前或者以后增加一些附加的功能。这样就能确保在运行时,不用修改给定对象结构就可以在外部增加附加的功能。在

                                               面向对象的程序设计中,通常是使用继承的关系来扩展给定类的功能。

                              8.    案例:

                                     1)    抽象的构建接口:

                                             packagecom.abao.decorate;

                                             public interface Component{

                                                         public void doSomething();

                                            }

                                      2)    具体的构建角色:

                                             packagecom.abao.decorate;

                                             public class ConcreteComponent implements Component{

                                                    @Override

                                                     public void doSomething() {

                                                            System.out.println("功能A");

                                                       }

                                              }

                                     3)    装饰角色:

                                            packagecom.abao.decorate;

                                            public class Decorate implements Component{

                                                   private Component component;

                                                   public Decorate(Component component){

                                                         this.component = component;

                                                    }

 

                                                  @Override

                                                  public void doSomething(){

                                                            component.doSomething();

                                                   }

                                           }

                                  4)    具体装饰角色1

                                         packagecom.abao.decorate;

                                         public class ConcreteDecorate1 extends Decorate{

                                               public ConcreteDecorate1(Component component){

                                                        super(component);

                                               }

                                               @Override

                                               public void doSomething(){

                                                        super.doSomething();

                                                        this.doAnotherDosomething();

                                               }

                                               private void doAnotherDosomething(){

                                                        System.out.println("功能B");

                                               }

                                        }

                               5)    具体装饰角色2

                                      packagecom.abao.decorate;

                                      public class ConcreteDecorate2 extends Decorate{

                                             public ConcreteDecorate2(Component component){

                                                        super(component);

                                             }

                                             @Override

                                              public void doSomething(){

                                                       super.doSomething();

                                                       this.doAnotherDosomething();

                                              }

                                             private void doAnotherDosomething(){

                                                       System.out.println("功能C");

                                             }

                                       }

                             6)    客户端

                                     packagecom.abao.decorate;

                                     public class Client{

                                             public static void main(String[] args) {

                                                    Component component = new ConcreteDecorate1(

                                                          new ConcreteDecorate2(new ConcreteComponent()));

                                                    component.doSomething();

                                              }

                                      }

                         9.    完!



二、Java装饰模式
装饰模式:给一个类添加一些额外的职责,并且在添加这些额外的职责时不会控制该类的执行逻辑。

UML类图:


组成部分:
抽象构件:原始的功能接口
具体构件:具体的原始功能类
装饰角色:持有具体构件类的对象,以便执行原有功能
具体装饰:具体扩展的功能在这里

下面看一个对开车功能拓展的实例(晚上+开车):
抽象构件:

package com.gjy.drector;

/**
 * 抽象接口,规范准备接收附加责任的对象
 * @author gjy
 */
public interface Component {
	public void operation();
}

具体构件:
package com.gjy.drector;

/**
 * 接收附加责任, 此类型的类可以有多个, 只对应一个Decorator类
 * @author gjy
 */
public class ConcreteComponent implements Component {
	public ConcreteComponent(){}
    public void operation()
    {
           System.out.println("开车");
    }
}

装饰角色:
package com.gjy.drector;

/**
 * 装饰角色,持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口
 * @author gjy
 */
public class Decorator implements Component {
	private Component component;
    public Decorator(){}

    public Decorator(Component component)
    {
           this.component = component;
    }

    public void operation() {
           component.operation();
    }
}

具体装饰:
package com.gjy.drector;

/**
 * 添加附加责任
 * @author gjy
 */
public class ConcreteDecorator extends Decorator {

	public ConcreteDecorator(){}

    public ConcreteDecorator(Component component)
    {
           super(component);
    }

    public void operation()
    {
    	 this.addedOperation();
         super.operation();
    }

    public void addedOperation()
    {
           System.out.println("晚上");
    }
}

测试:
package com.gjy.drector;

/**
 * 客户端类
 * @author gjy
 */

public class Client {
       public static void main(String[] args) {
              Component component = new ConcreteComponent();
              Decorator decorator = new ConcreteDecorator(component);
              //客户端不变, 但已增加了责任
              decorator.operation();
              
       }
}

输出结果:
晚上
开车

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值