33

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?

Ravindra babu
  • 45,953
  • 8
  • 231
  • 206
jb.
  • 787
  • 1
  • 7
  • 14
  • 5
    See [this post](http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns/2707195#2707195) for a comprehensive list of GoF patterns used in the Java standard libraries. – Abhinav Sarkar Jun 18 '10 at 11:08
  • 1
    Check this post at Java Code Geeks about design patterns in JDK - http://www.javacodegeeks.com/2011/03/design-patterns-in-jdk.html – jb. Mar 16 '11 at 08:45

5 Answers5

33

BufferedReader etc implements decorator pattern. Any Reader, e.g. FileReader or StringReader, can be decorated with the buffering feature, which is really source-oblivious.


Other patterns


Anti-patterns

To add to what others have said, these are several anti-patterns in the Java libraries:

Antipattern: inheritance instead of composition

From Effective Java 2nd Edition, Item 16: Favor composition over inheritance:

There are a number of obvious violations of this principle in the Java platform libraries. For example, a stack is not a vector, so Stack should not extend Vector. Similarly, a property list is not a hash table, so Properties should not extend Hashtable. In both cases, composition would have been preferable.

Related questions


Antipattern: constant interfaces

From Effective Java 2nd Edition, Item 19: Use interfaces only to define types:

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.

Related questions


Antipattern: telescoping constructor and JavaBeans patterns

From Effective Java 2nd Edition, Item 2: Consider a builder when faced with many constructor parameters (excerpt online):

Traditionally, programmers have used the telescoping constructor pattern, 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 JavaBeans pattern, 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.

Bloch recommends using a builder pattern instead.

Related questions

Community
  • 1
  • 1
polygenelubricants
  • 364,035
  • 124
  • 554
  • 617
27

I guess they wanted to hear about the Decorator pattern which can be found in the various Streams, Readers and Writers.

Other patterns (small selection):

I'm pretty sure that one can find examples for almost all patterns listed on this wikipedia page in the Java SDK.

Andreas Dolk
  • 111,016
  • 17
  • 174
  • 259
8

The decorator pattern is often used in java i/o.

Example

BufferedReader br = new BufferedReader(new FileReader("filename.txt")); 
stacker
  • 66,311
  • 27
  • 138
  • 208
4

Decorator pattern, I think. To create all flavors of Readers, Writers, input and output streams. See this, for example.

Marat Salikhov
  • 6,137
  • 4
  • 30
  • 34
1

Patterns used in java.io package.

  1. Decorator_pattern.

    Examples:

    The abstract class java.io.FilterInputStream and its concrete subclasses : BufferedInputStream, CheckedInputStream etc

  2. Abstract_factory_pattern and Factory_method_pattern patterns:

    Examples:

    The abstract class InputStream and its concrete sub classes: ByteArrayInputStream, FileInputStream, FilterInputStream etc.

    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.StringBufferInputStream
    
  3. Adapter_pattern:

    Examples:

    java.io.InputStreamReader translates a byte stream into a character stream, and a java.io.OutputStreamWriter translates a character stream into a byte stream.

    You can find some more details in this article

  4. Template_method_pattern (source: journaldev article)

    All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.

For all other patterns in java, refer to this post:

Examples of GoF Design Patterns in Java's core libraries

Community
  • 1
  • 1
Ravindra babu
  • 45,953
  • 8
  • 231
  • 206