本文介绍了IO 的 GoF 装饰器模式的用例和示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 wikipedia 中读到 Decorator 模式 用于.NetJava IO 类.

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

谁能解释一下这是如何使用的?举个可能的例子有什么好处?

Can anybody explain how this is being used? And what is the benefit of it with a possible example?

维基百科上有一个 Windows 表单 示例,但我想知道 Java IO 类是如何发生的.

There is an example of Windows forms on wikipedia but I want to know how it happens with Java IO classes.

推荐答案

InputStream 是一个抽象类.大多数具体实现,如 BufferedInputStream, GzipInputStreamObjectInputStream 等有一个构造函数,它采用 相同 抽象类的实例.这就是装饰器模式的识别关键(这也适用于采用相同接口实例的构造函数).

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.

假设我们在一个 Gzipped 文件中有一堆序列化的 Java 对象,并且我们想要快速读取它们.

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);

文件被gzipped,所以我们需要解压它:

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();
// ...

好处是您可以自由地使用一个或多个不同的装饰器来装饰流以满足您的需要.这比为每个可能的组合使用一个类要好得多,例如 ObjectGzipBufferedFileInputStreamObjectBufferedFileInputStreamGzipBufferedFileInputStreamObjectGzipFileInputStreamObjectFileInputStreamGzipFileInputStreamBufferedFileInputStream

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();

另见:

10-10 22:24