问题描述
我在 wikipedia 中读到装饰器模式用于 .Net 和 Java IO 类.
有人可以解释这是如何使用的吗?举个可能的例子有什么好处?
wiki 上有一个窗口窗体示例,但我想知道 IO 类是如何发生的.
InputStream 是一个抽象类.最具体的实现,例如 BufferedInputStream、GzipInputStream, ObjectInputStream 等有一个构造函数,它采用 same 抽象类的实例.这是装饰器模式的识别键(这也适用于构造函数采用相同接口的实例).
当使用这样的构造函数时,所有方法都将委托给包装的实例,方法的行为方式会发生变化.例如,预先在内存中缓冲流,预先解压缩流或以不同方式解释流.有些甚至有额外的方法,最终也进一步委托给包装的实例.这些方法用额外的行为装饰被包装的实例.
假设我们在一个 Gzip 文件中有一堆序列化的 Java 对象,并且我们想快速读取它们.
首先打开它的一个输入流:
FileInputStream fis = new FileInputStream("/objects.gz");
我们想要速度,所以让我们在内存中缓冲它:
BufferedInputStream bis = new BufferedInputStream(fis);
文件是gzip压缩的,所以我们需要解压缩它:
GzipInputStream gis = new GzipInputStream(bis);
我们需要反序列化这些 Java 对象:
ObjectInputStream ois = new ObjectInputStream(gis);
现在我们终于可以使用它了:
SomeObject someObject = (SomeObject) ois.readObject(); // ...
好处是您可以自由地使用一个或多个不同的装饰器来装饰流以满足您的需求.这比为每个可能的组合(如 ObjectGzipBufferedFileInputStream、ObjectBufferedFileInputStream、GzipBufferedFileInputStream、ObjectGzipFileInputStream、ObjectFileInputStream、GzipFileInputStream、BufferedFileInputStream 等)设置一个类要好得多.
请注意,当您要关闭流时,只需关闭 outermost 装饰器就足够了.它将一直委托关闭调用到底部.
ois.close();
另见:
问题描述
I have read in wikipedia that Decorator pattern is used for .Net and Java IO classes.
Can someone explain how this is being used? and what s the benefit of it with a possible example?
There is an example of window forms on wiki but i wanted to know how it happens with IO classes.
推荐答案
InputStream is an abstract class. Most concrete implementations like BufferedInputStream, GzipInputStream, ObjectInputStream, etc. have a constructor that takes an instance of the same abstract class. That's the recognition key of the decorator pattern (this also applies to constructors taking an instance of the same interface).
When such a constructor is used, all methods will delegate to the wrapped instance, with changes in the way the methods behave. For example, buffering the stream in memory beforehand, decompressing the stream beforehand or interpreting the stream differently. Some even have additional methods that finally also delegate further to the wrapped instance. Those methods decorate the wrapped instance with extra behaviour.
Let's say that we have a bunch of serialized Java objects in a Gzipped file and that we want to read them quickly.
First open an inputstream of it:
FileInputStream fis = new FileInputStream("/objects.gz");
We want speed, so let's buffer it in memory:
BufferedInputStream bis = new BufferedInputStream(fis);
The file is gzipped, so we need to ungzip it:
GzipInputStream gis = new GzipInputStream(bis);
We need to unserialize those Java objects:
ObjectInputStream ois = new ObjectInputStream(gis);
Now we can finally use it:
SomeObject someObject = (SomeObject) ois.readObject(); // ...
The benefit is that you have a lot of freedom to decorate the stream using one or more various decorators to suit your needs. That's much better than having a single class for every possible combination like ObjectGzipBufferedFileInputStream, ObjectBufferedFileInputStream, GzipBufferedFileInputStream, ObjectGzipFileInputStream, ObjectFileInputStream, GzipFileInputStream, BufferedFileInputStream, etc.
Note that when you're about to close the stream, just closing the outermost decorator is sufficient. It will delegate the close call all the way to the bottom.
ois.close();