用于IO的GoF Decorator模式的用例和示例 [英] Use Cases and Examples of GoF Decorator Pattern for IO

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

问题描述

我已在维基百科中读到装饰器模式用于 .Net Java IO 类。



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



维基百科上有一个 Windows表单的例子,但我想知道它是怎么回事发生在 Java IO 类中。

解决方案

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



当使用这样的构造函数时,所有方法都将委托给包装的实例,方法的行为方式发生了变化。例如,事先在内存中缓冲流,预先解压缩流或不同地解释流。有些甚至还有其他方法最终也会进一步委托给包装的实例。这些方法用额外的行为来装饰包装的实例。



假设我们在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 等。



请注意,当您即将关闭流时,只需关闭最外面的装饰器就足够了。它会将近距离通话委托给底部。

  ois.close(); 



参见:




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?

There is an example of Windows forms on wikipedia but I want to know how it happens with Java 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();

See also:

这篇关于用于IO的GoF Decorator模式的用例和示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆