修饰模式的IO模式、IO

2023-09-02 11:45:52 作者:心软脾气硬

我读过的维基的装饰图案用于.NET和Java IO类。

I have read in wikipedia that Decorator pattern is used for .Net and Java IO classes.

有人能解释一下这是如何被使用的?什么的S它与一个可能的例子有什么好处?

Can someone explain how this is being used? and what s the benefit of it with a possible example?

还有就是在维基窗口形成了一个例子,但我想知道它与IO类怎么回事了。

There is an example of window forms on wiki but i wanted to know how it happens with IO classes.

推荐答案

的InputStream 是一个抽象类。最具体的实现,如 的BufferedInputStream , < $ C C> GzipInputStream ,的 的ObjectInputStream ,等有一个构造函数,需要在同一个抽象类的一个实例。这是Decorator模式的识别键(这也适用于构造采取同样的接口的实例)。

InputStream is an abstract class. Most concrete implementations like BufferedInputStream, GzipInputStream, ObjectInputStream, etc have a constructor which 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).

当已经用了这样一个构造函数,那么所有方法都将委托给包裹的实例,在这里和那里的方式方法的行为变化。例如,缓冲存储器中的数据流预先,或者DECOM $ P $预先pssing流,或帧间preting流不同,等等。有些人甚至更多的方法,最终也进一步委托给被包装的实例。这些方法装饰包裹的情况下提供额外的行为。

When such a constructor is been used, then all methods will delegate to the wrapped instance, with here and there changes in the way how the methods behave. For example, buffering the stream in memory beforehand, or decompressing the stream beforehand, or interpreting the stream differently, etcetera. Some have even additional methods which finally also delegate further to the wrapped instance. Those methods decorate the wrapped instance with extra behaviour.

让我们说,我们有一大堆的序列化的Java对象的一个​​gzip压缩的文件,我们希望快速阅读了一下。

Let's say that we have a bunch of serialized Java objects in a Gzipped file and that we want to read them a bit quickly.

首先打开的是一个InputStream:

First open an inputstream of it:

FileInputStream fis = new FileInputStream("/objects.gz");

我们希望speeeed,所以让我们缓存在内存中:

We want speeeed, so let's buffer it in memory:

BufferedInputStream bis = new BufferedInputStream(fis);

该文件gzip压缩,所以我们需要展开它:

The file is gzipped, so we need to ungzip it:

GzipInputStream gis = new GzipInputStream(bis);

我们需要反序列化的Java对象:

We need to unserialize those Java objects:

ObjectInputStream ois = new ObjectInputStream(gis);

现在,我们终于可以使用它:

Now we can finally use it:

SomeObject someObject = (SomeObject) ois.readObject();
// ...

这样做的好处是,你有pretty的大量自由装饰使用一个或多个不同的装饰,以满足您的需求流。这比将一个类如 ObjectGzipBufferedFileInputStream ObjectBufferedFileInputStream GzipBufferedFileInputStream各种可能的组合要好得多 ObjectGzipFileInputStream ObjectFileInputStream GzipFileInputStream BufferedFileInputStream ,etc..etc ..

The benefit is that you have pretty plenty 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..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();

参见:

Examples在Java的核心库 GoF的设计模式

See also:

Examples of GoF Design Patterns in Java's core libraries