FilterInputStream 是 InputStream 的一种,具有 InputStream。

huangapple go评论77阅读模式
英文:

FilterInputStream IS-A InputStream and HAS-A InputStream

问题

我正在阅读Java I/O库的源代码,注意到FilterStream扩展了抽象类InputStream,倾向于使用继承,并且还有一个作为实例变量的InputStream,倾向于使用组合。为什么我们需要同时具有InputStream的IS-A和HAS-A关系?

英文:

I am reading the source code of Java I/O lib and noticed that FilterStream extends the abstract class InputStream favouring inheritance and also has a InputStream as an instance variable favoring composition.Why do we need a IS-A and a HAS-A relationship at the same time with InputStream ?

答案1

得分: 3

FilterInputStream是一个输入流,它可以添加或修改现有输入流的行为,您需要传入该流。例如,它的子类BufferedInputStream添加了缓冲功能。由于FilterInputStream需要持有您传入的流,因此还存在着“有一个”关系。

FilterInputStream装饰器模式的一个示例:https://en.wikipedia.org/wiki/Decorator_pattern

英文:

FilterInputStream "is an" input stream that adds or modifies the behavior of an existing input stream that you need to pass in. For example, its subclass BufferedInputStream adds a buffering capability. Since FilterInputStream needs to hold on to the stream you pass in, there is also a "has a" relationship.

FilterInputStream is an example of the decorator pattern: https://en.wikipedia.org/wiki/Decorator_pattern

huangapple
  • 本文由 发表于 2020年7月26日 18:38:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63099020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定