Java IO

http://tutorials.jenkov.com/java-io/index.html

从Decorator设计模式的角度解读InputStream类家族

装饰器设计模式的基本元素是 Component, ConcreteComponent, Decorator. ConcreteComponent和Decorator都会继承Component。除此之外,Decorator还组合了Component,也就是说Decorator对象内部会有个Component的引用。

那么

  • InputStream的角色是Component;
  • FileInputStream(来自文件系统的数), ByteArrayInputStream(来自内存字节数组的数据) , PipedInputStream(来自其他进程的数据)和StringBufferInputStream(来自字符串的数据,只是已经Deprecated了)的角色就是ConcreteComponent
  • 除了SequenceInputStream,其他的各种InputStream Class都是Decorator的角色。

字节和字符流之间的联系和交互

  • InputStreamReader是从字节流到字符流的桥梁
  • OutputStreamWriter是从字符流到字节流的桥梁

results matching ""

    No results matching ""