Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most used pattern in java.io?

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?

like image 951
jb. Avatar asked Jun 18 '10 10:06

jb.


People also ask

What is the most used design pattern in java?

Factory Design Pattern One of the most popular design patterns used by software developers is a factory method. It is a creational pattern that helps create an object without the user getting exposed to creational logic.

What pattern does java IO use?

Decorator Pattern This is a common pattern found in Java's original IO classes used for reading and writing to sources outside JVM. For example, the InputStream and the OutputStream classes and their subclasses extensively use this pattern in their read and write operations.

Which design pattern is mostly used?

Adapter (Structural) The adapter pattern (or sometimes known as a wrapper) is one of the most useful and most popular design patterns in software engineering. This pattern seeks to solve the problem of incompatible interfaces between a client and a service provider.

Does java IO use strategy pattern?

io. FilterOutputStream.) The Strategy pattern has been used in class java.


2 Answers

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

  • java.util.Comparator<T> is a strategy pattern

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

  • 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

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

  • Should a class implement a constants-only interface?
  • What is the best way to implement constants in Java ?

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

  • Is this a well known design pattern? What is its name?
  • What is the difference between Builder Design pattern and Factory Design pattern?
  • When would you use the Builder Pattern?
like image 130
polygenelubricants Avatar answered Oct 16 '22 16:10

polygenelubricants


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):

  • Observer pattern in the swing libraries
  • Factory pattern in 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 page in the Java SDK.

like image 33
Andreas Dolk Avatar answered Oct 16 '22 18:10

Andreas Dolk