IO的装饰模式 [英] Decorator Pattern for IO

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

问题描述

我在维基百科中阅读了Decorator模式用于.Net和Java IO类。



有人可以解释这是如何使用的?那么可能的例子有什么好处呢?



在wiki上有一个窗口窗体的例子,但是我想知道IO类是如何发生的。 p>

解决方案

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



当使用这样的构造函数时,所有方法都将委托包装的实例,随着方法行为的变化。例如,预先在存储器中缓冲流,解压缩流或不同地解释流。有些甚至有额外的方法,最后还将委托进一步包装的实例。这些方法用额外的行为来装饰包装的实例。



假设我们在Gzipped文件中有一堆序列化的Java对象,并且我们想要快速阅读它们。 / p>

首先打开一个输入流:

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

See also:

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

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