英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论