java.io 中最常用的模式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3068912/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the most used pattern in java.io?
提问by jb.
I was asked this question recently during my job interview, and I couldn't answer it. So, what is the most used pattern in java.io and how is it used? What are other patterns used in common java libraries?
最近面试的时候被问到这个问题,我答不上来。那么,java.io 中最常用的模式是什么,又是如何使用的呢?常见的 Java 库中使用的其他模式是什么?
采纳答案by Andreas Dolk
I guess they wanted to hear about the Decorator patternwhich can be found in the various Streams, Readers and Writers.
我猜他们想了解可以在各种 Streams、Readers 和 Writers 中找到的装饰器模式。
Other patterns (small selection):
其他图案(小选):
- Observer patternin the swing libraries
 - Factory patternin the javax.xml.parsers packages
 - Iterator pattern, used in collections
 
I'm pretty sure that one can find examples for almost all patterns listed on this wikipedia pagein the Java SDK.
我很确定可以在 Java SDK 中找到此维基百科页面上列出的几乎所有模式的示例。
回答by polygenelubricants
BufferedReaderetc implements decorator pattern. Any Reader, e.g. FileReaderor StringReader, can be decoratedwith the buffering feature, which is really source-oblivious.
BufferedReader等实现装饰器模式。任何Reader,例如FileReader或StringReader,都可以使用缓冲功能进行修饰,这实际上是源代码不明确的。
Other patterns
其他图案
Anti-patterns
反模式
To add to what others have said, these are several anti-patterns in the Java libraries:
补充一下其他人所说的,这些是Java 库中的几个反模式:
Antipattern: inheritance instead of composition
反模式:继承而不是组合
From Effective Java 2nd Edition, Item 16: Favor composition over inheritance:
来自Effective Java 2nd Edition,第 16 条:优先组合而不是继承:
There are a number of obvious violations of this principle in the Java platform libraries. For example, a stack is not a vector, so
Stackshould not extendVector. Similarly, a property list is not a hash table, soPropertiesshould not extendHashtable. In both cases, composition would have been preferable.
Java 平台库中有许多明显违反这一原则的地方。例如,堆栈不是向量,因此
Stack不应扩展Vector。同样,属性列表不是哈希表,因此Properties不应扩展Hashtable。在这两种情况下,组合都是可取的。
Related questions
相关问题
- Prefer composition over inheritance?
 - java inheritance versus composition (implementing a stack)
 - Difference between Inheritance and Composition
 - Inheritance or composition: Rely on “is-a” and “has-a”?
 - Object Oriented Best Practices - Inheritance v Composition v Interfaces
 - Should I use inheritance or composition?
 - Inheritance vs. Aggregation
 - Aggregation verses Composition
 - Decorator Pattern Using Composition Instead of Inheritance
 
- 更喜欢组合而不是继承?
 - java继承与组合(实现堆栈)
 - 继承与组合的区别
 - 继承还是组合:依靠“is-a”和“has-a”?
 - 面向对象的最佳实践 - 继承 v 组合 v 接口
 - 我应该使用继承还是组合?
 - 继承与聚合
 - 聚合与组合
 - 使用组合代替继承的装饰器模式
 
Antipattern: constant interfaces
反模式:常量接口
From Effective Java 2nd Edition, Item 19: Use interfaces only to define types:
来自Effective Java 2nd Edition,第 19 条:仅使用接口来定义类型:
There are several constant interfaces in the Java platform libraries, such as
java.io.ObjectStreamConstants. These interfaces should be regarded as anomalies and should not be emulated.
Java 平台库中有几个常量接口,例如
java.io.ObjectStreamConstants. 这些接口应被视为异常,不应被模拟。
Related questions
相关问题
- Should a class implement a constants-only interface?
 - What is the best way to implement constants in Java ?
 
Antipattern: telescoping constructor and JavaBeans patterns
反模式:伸缩构造函数和 JavaBeans 模式
From Effective Java 2nd Edition, Item 2: Consider a builder when faced with many constructor parameters(excerpt online):
来自Effective Java 2nd Edition,第 2 项:当面对许多构造函数参数时考虑构建器(摘录在线):
Traditionally, programmers have used the telescoping constructorpattern, in which you provide a constructor with only the required parameters, another with a single optional parameters, a third with two optional parameters, and so on [...] The telescoping constructor pattern works, but it is hard to write client code when there are many parameters, and harder still to write it.
A second alternative when you are faced with many constructor parameters is the JavaBeanspattern, in which you call a parameterless constructor to create the object, and then call setter methods to set each required parameter, and each optional parameter of interest. [...] Unfortunately the JavaBeans pattern has serious disadvantages of its own [...] a JavaBean may be in an inconsistent state partway through its construction [and it] precludes the possibility of making a class immutable.
传统上,程序员使用伸缩构造函数模式,在该模式中,您提供一个仅具有必需参数的构造函数,另一个具有单个可选参数,第三个具有两个可选参数,依此类推 [...] 伸缩构造函数模式有效,但是参数多的时候客户端代码很难写,更难写。
当您面对许多构造函数参数时,第二种选择是JavaBeans模式,在该模式中,您调用无参数构造函数来创建对象,然后调用 setter 方法来设置每个必需参数和每个感兴趣的可选参数。[...] 不幸的是,JavaBeans 模式有其自身的严重缺点 [...] JavaBean 在其构造过程中可能处于不一致的状态 [并且它] 排除了使类不可变的可能性。
Bloch recommends using a builder patterninstead.
Bloch 建议改用构建器模式。
Related questions
相关问题
回答by stacker
The decorator patternis often used in java i/o.
的装饰图案常常在Java I / O使用。
Example
例子
BufferedReader br = new BufferedReader(new FileReader("filename.txt")); 
回答by viaclectic
回答by Ravindra babu
Patterns used in java.io package.
java.io 包中使用的模式。
Examples:
The abstract class java.io.FilterInputStreamand its concrete subclasses :
BufferedInputStream, CheckedInputStreametcAbstract_factory_patternand Factory_method_patternpatterns:
Examples:
The abstract class InputStreamand its concrete sub classes:
ByteArrayInputStream, FileInputStream, FilterInputStreametc.InputStream input = new FileInputStream("some_file.txt");Below classes enable unification of input from a byte-array, a file, a network connection, a persistent storage, a pipe, a string, etc:
class java.io.InputStream class java.io.ByteArrayInputStream class java.io.FileInputStream class java.io.FilterInputStream class java.io.ObjectInputStream class java.io.PipedInputStream class java.io.SequenceInputStream class java.io.StringBufferInputStreamExamples:
java.io.InputStreamReadertranslates a byte stream into a character stream, and ajava.io.OutputStreamWritertranslates a character stream into a byte stream.You can find some more details in this article
Template_method_pattern(source: journaldevarticle)
All non-abstract methods of
java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
例子:
抽象类java.io.FilterInputStream及其具体子类:
BufferedInputStream, CheckedInputStream等Abstract_factory_pattern和Factory_method_pattern模式:
例子:
抽象类InputStream及其具体子类:
ByteArrayInputStream, FileInputStream, FilterInputStream等。InputStream input = new FileInputStream("some_file.txt");下面的类可以统一来自字节数组、文件、网络连接、持久存储、管道、字符串等的输入:
class java.io.InputStream class java.io.ByteArrayInputStream class java.io.FileInputStream class java.io.FilterInputStream class java.io.ObjectInputStream class java.io.PipedInputStream class java.io.SequenceInputStream class java.io.StringBufferInputStream例子:
java.io.InputStreamReader将字节流转换为字符流,ajava.io.OutputStreamWriter将字符流转换为字节流。您可以在本文中找到更多详细信息
Template_method_pattern(来源:journaldev文章)
的所有非抽象方法
java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer。
For all other patterns in java, refer to this post:
对于 Java 中的所有其他模式,请参阅此帖子:

