装饰者模式的详解
装饰者模式动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性
的替代方案。
装饰者模式设计类之间的关系:
其中Component是一个超类,ConcreteComponen是被装饰者,Decorator是装饰者,装饰者和被装饰者有共同的超类Component,可是此时咱们发现Decorator和Component还有另一条线,这条线表示Decorator还要组合Component。
在我看来装饰的模式中这几个类的主要做用以下:
首先咱们看一个这样的现象(这个是我在网上看到的例子,我把它引入进来主要是方便谈谈个人理解)
现象描述:
若是说继承里的超类是一个模具(Component),作出各类各样的稍有不一样的子类成品(ConcreteComponen,ConcreteDecoratorA,ConcreteDecoratorB)来知足各类各样的功能。那么把装饰模式比做的一条生产线吧,一个产品(ConcreteComponen)传过来,在各道工序不断地给它加上新的功能,把它一步步按照顺序包装成一个全新的产品(Concrete DecoratorA,Concrete DecoratorB)。记住,这条生产线多是无限长的,这种包装也能够是无限添加的。
个人理解
1. Component是超类,他出现的意义是什么呢?照理说Decorator直接去组合ConcreteComponent,就能够去增强ConcreteComponent的行为,成而加工出一个不通的子类成品。可是可能有这样一种状况,ConcreteComponent的平级的区域内还有ConcreteComponent1,ConcreteComponent2……这样要为每个ConcreteComponent都去写一个Decorator吗?这样显然不可能。因此抽象出Component的一个好处就是让ConcreteComponent和Decorator彼此解耦。Decorator只要获得Component对象的引用便可。
2. Decorator是装饰者,而真正装饰的过程是在Decorator的子类ConcreteDecoratorA和ConcreteDecoratorB中完成的,他们使用不一样的装饰方法,做出了不一样的子类成品。Decorator的做用只是告诉你们,我要去装饰ConcreteComponent,或许还会提供一些用这个装饰者去装饰的而造成的成品共用的特性和功能。
3. 装饰者永远是装饰者,但装饰者装饰出来的成品也有可能会变成被装饰者。即ConcreteDecoratorA和ConcreteDecoratorB有一天也会变到ConcreteComponen的地位。
只不过这个时候在ConcreteDecoratorA的旁边可能会出现一个相似于Decorator的装饰者DecoratorA,它一样要继承并组合超类Component。目的是相同的:解耦,让一个装饰者没必要为一个被装饰者而存在
这时候的装饰者模式的结构图变成:
由上图说明一个道理,若是条件允许的话Decorator和DecoratorA的位置是能够互相交换的。这就像冲一杯带糖的奶粉,Decorator表示放糖,DecoratorA表示放奶粉,实际上是先放糖仍是先放奶粉,最终都能获得ConcreteDecoratorAA(一杯带糖的奶粉)。固然这是在条件允许的状况下,在有些生产环境中,制作工序(装饰的前后)是不能乱的。
装饰者模式在JAVA IO中的应用
先给出Java IO包中主要类的关系:
java IO包中有四大等级结构InputStream,outputStream, InputStreamReader,outputStreamReader。InputStream和OutputStream处理8位字节流数据, Reader和Writer处理16位的字符流数据。InputStream和Reader处理输入, OutputStream和Writer处理输出。各个体系内部用到的都是装饰者模式,而InputStream和InputStreamReader之间,outputStream和outputStreamReader之间用的是适配器模式
下面主要以InputStream和InputStreamReader为例详解
1. 从装饰者模式看InputStream结构
InputStream的类图关系
class java.lang.Object
|
|—class java.io.InputStream //输入流,字节形式,为如下的基类
| |
| |——ByteArrayInputStream //从字节数组中读取
| |
| |——FileInputStream //从文件中读取数据
| |
| |—— FilterInputStream //过滤流的基类,
| | | // 过滤能够了解为各类处理技术的形象称呼
| | |
| | |——BufferedInputStream //缓冲技术,
| | | // 数据来自底层输入流
| | |
| | |——DataInputStream //可读java数据类型
| | |
| | |——PushbackInputStream //缓冲技术,
| | | // 数据来自任意输入流
| | |
| | |——java.util.zip.GZIPInputStream
| | | //不是java.io包中的流。压缩技术
| | |
| | |——java.security.DigestInputStream
| | | //不是java.io包中的流。处理流的摘要
| | |
| |—— .......
从图中能够看出,InputStream就是装饰者模式中的超类(Component),ByteArrayInputStream,FileInputStream至关于被装饰者(ConcreteComponent),这些类都提供了最基本的字节读取功能。
而另一个和这两个类是同一级的类FilterInputStream便是装饰者(Decorator),BufferedInputStream,DataInputStream,PushbackInputStream…这些都是被装饰者装饰后造成的成品。
根据装饰者模式的特色,咱们能够总结出这些IO流的使用方法:
File file = new File ("hello.txt");
FileInputStream in=new FileInputStream(file);
BufferedInputStream inBuffered=new BufferedInputStream (in);
这里BufferedInputStream主要是提供了缓存机制,先读入一个byte[],等count到达缓存Byte[]的大小的时候,再一次读入。
固然你也能够写成BufferedInputStream inBuffered =
new BufferedInputStream (new FileInputStream(new File ("hello.txt")));
从使用的角度来看装饰者模式,能够看出它的一个缺点:装饰者模式的实现对于使用者是透明的,当使用者不熟悉你的实现的时,就很难理解。
同理你能够学习一下另一个结构outputStream
2. 适配器模式看InputStreamReader
适配器模式比较简单就很少讲了,主要是解决了java没法多继承的问题,下面大概讲一下IO包中是怎么用这个模式的,用它来作什么?
InputStreamReader和InputStream的功能的不一样点在于InputStream是以二进制输入 / 输出, I/O 速度快且效率高,因为读到的是字节,也就不存在乱码问题,平台移植性好。可是它的 read ()方法读到的是一个字节,很不利于人们阅读。InputStreamReader类将字节转换为字符。 你能够在构造器中指定编码的方式,若是不指定的话将采用底层操做系统的默认编码方式。
Java.io.Reader类提供了要求了继承这个类的全部类必须提供
/**
* Reads characters into a portion of an array. This method will block
* until some input is available, an I/O error occurs, or the end of the
* stream is reached.
*
* @param cbuf Destination buffer
* @param off Offset at which to start storing characters
* @param len Maximum number of characters to read
*
* @return The number of characters read, or -1 if the end of the
* stream has been reached
*
* @exception IOException If an I/O error occurs
*/
abstract public int read(char cbuf[], int off, int len) throws IOException;
代码注释理解:读出来的形式必须是字符,而不是字节了,InputStreamReader继承于Reader,即具有了读出字符的功能,而把什么读成字节的功能就要InputStreamReader去适配了,InputStreamReader的构造函数是这样的:
/**
* Creates an InputStreamReader that uses the named charset.
*
* @param in
* An InputStream
*
* @param charsetName
* The name of a supported
* {@link java.nio.charset.Charset </code>charset<code>}
*
* @exception UnsupportedEncodingException
* If the named charset is not supported
*/
public InputStreamReader(InputStream in, String charsetName)
throws UnsupportedEncodingException
{
super(in);
if (charsetName == null)
throw new NullPointerException("charsetName");
sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
}
用的是InputStream去适配InputReader。
3. 浅谈Reader体系
Reader体系中一样用到的是装饰者模式,可是有一点不一样,Reader体系中的FilterRead类和InputStream体系中的FilterInputStream的功能不一样,它再也不是装饰者。
这一点能够从BufferReader和BufferStreamReader的实现不一样能够看出
bufferReader:public class BufferedReader extends Reader
BufferedInputStream:public class BufferedInputStream extends FilterInputStream
但模式仍是相同的。在Reader我没找到FilterInputStream相似做用的东西
下面看看Reader IO的使用方法
1. File file = new File ("hello.txt");
2. FileInputStream in=new FileInputStream(file);
3. InputStreamReader inReader=new InputStreamReader(in);
4. BufferedReader bufReader=new BufferedReader(inReader);
能够看出步骤2到3使用的是适配器模式,而3到4使用的是装饰者模式
好了,以上就是我学习设计模式和java IO的心得,本身也是初学者,但愿对一样这块不太了解的人有所帮助,有什么意见你们能够提出。java