码迷,mamicode.com
首页 > 编程语言 > 详细

06JavaIO详解_IO流中的设计模式-装饰者模式

时间:2016-08-05 23:08:53      阅读:483      评论:0      收藏:0      [点我收藏+]

标签:

Io流里面的过滤流和节点流用的就是装饰者模式。整个的IO体系就是装饰模式。

技术分享  

 

这个写法就是装饰者模式。对上面的三个已经存在的类(DataOutputStream,BufferedOutputStream,FileOutputStream)进行组合,具有三种功能。

 

技术分享

技术分享

 

 

对于DataOutputStream dos=new DataOutStream(new BufferedOutputStream(new FileOutputStream("data.txt")));

这行代码来说。

OutPutStream是抽象构建角色。因为这三个类都是继承于OuputStream抽象类的。

FileOutputStream是具体构建角色(真实角色)(节点流)。

FilterOutPutStream和FilterInputStream是装饰角色。因为在Io体系中,BufferedOutputStream,DataOutStream都是继承自FilterOutPutStream抽象类的。

BufferedOutputStream,DataOutStream是具体装饰角色。

 

装饰模式的特点:

 1.装饰对象和真实对象有相同的接口。这句话是说,不管是BufferedOutputStream(装饰角色)还是FileOutputStream(真实角色)都是继续自OutPutStream.

 2.装饰角色包含一个真实对象的引用。

3.装饰对象接收所有来自客户端的请求,再把请求交给真实对象。

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

技术分享

 

实现自己的装饰模式:

案例结构如下:

技术分享

 

上图中:Client是测试端。

          Component是抽象构建角色,相当于OutputStream.

          ConcreteComponent是具体构建角色(真实角色),也就是IO流中的节点流,相当于FileOutputStream.

          Decorator是抽象构建角色,相当于过滤流的父类,也就是相当于IO流中的FilterOutputStream.

         ConcreteDecorator1相当于IO流中的过滤流,也就是这篇文章中的BufferedOutputStream.

          ConcreteDecorator2相当于IO流中的过滤流,也就是这篇文章中的DataOutputStream.

具体代码如下:

Component

package com.guigu.shen.ConcreteComponent;
//抽象构建角色
public interface Component {
public void dosome();
}

ConcreteComponent:

package com.guigu.shen.ConcreteComponent;
//具体构建角色(就是真实角色,要被修饰的角色)
public class ConcreteComponent implements Component {


    public void dosome() {
        System.out.println("功能A");
        
    }

}

Decorator:

package com.guigu.shen.ConcreteComponent;
/*
 * 装饰角色
 * 1.要实现抽象构建角色
 * 2.要含有抽象构建角色的引用。
 * 
 */
public class Decorator implements Component {
private Component component;
    public Decorator(Component component) {
        this.component=component;
    }
    
    @Override
    public void dosome() {

        this.component.dosome();
        
    }

}

 

ConcreteDecorator1:

package com.guigu.shen.ConcreteComponent;
//具体装饰角色1
public class ConcreteDecoraror1 extends Decorator {
    public ConcreteDecoraror1(Component component) {
        super(component);
        
    }

    @Override
    public void dosome() {
        // TODO Auto-generated method stub
        super.dosome();
        doAnotherThing();
    }
    public void doAnotherThing()
    {
        System.out.println("功能B");
        
    }

    
    
}

  ConcreteDecorator2:

package com.guigu.shen.ConcreteComponent;
//具体装饰角色2
public class ConcreteDecorator2 extends Decorator {
    

    public ConcreteDecorator2( Component component) {
        super(component);
    }
    
    
    
    @Override
    public void dosome() {
        super.dosome();
        this.doAnotherThing();
    }
public void doAnotherThing()
{
    
System.out.println("功能C");
}
}

 

Client:

package com.guigu.shen.ConcreteComponent;

public class Client {
public static void main(String[] args) {
//1.先建立具体构建角色也就是真实角色(相当于节点流)
    Component component=new ConcreteComponent();
//2.构建第一个装饰角色
    Component component2=new ConcreteDecoraror1(component);
    //3.构建第二个装饰角色
    Component component3=new ConcreteDecorator2(component2);
    component3.dosome();
}
}

结果:

   功能A

   功能B

   功能C

你看。我们的本来功能是功能A,但是我们给他又加了两个功能,功能B和功能C。

 

对于上面程序的执行流程解释如下:

当我们执行component3.dosome时——>

                                                 执行:ConcreteDecorator2中的dosome()方法。

                                                           dosome()方法是:

                                 @Override
                    public void dosome() {
                                super.dosome();
                                this.doAnotherThing();
                                                                    }

     很明显要执行ConcreteDecoator2的父类Decorator的dosome方法。

                                               我们看一下Decorator的dosome方法的方法。

                                                  public Decorator(Component component) {
                                                          this.component=component;
                                                                     }
    
                                                             @Override
                                                          public void dosome() {

                                                                 this.component.dosome();
        
                                                                          }

 

很明显先要执行this.component=component里面的dosome方法。也就是公共A。

   然后就是就是B

     最后是C

  这个过程好好看看。

 

06JavaIO详解_IO流中的设计模式-装饰者模式

标签:

原文地址:http://www.cnblogs.com/shenxiaoquan/p/5742906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!