Saturday, August 19, 2017

Which of the following classes in java library do not implement a design pattern?

Leave a Comment

Today, I came across this weird interview question. Which of the following classes in java library do not implement a design pattern?

  1. Singleton
  2. Observer
  3. Iterator
  4. Proxy

Does anyone have a clue? I doubt the correctness of the question, but wanted to know community opinion. If there is something I am not aware of, as none of the options make sense.

3 Answers

Answers 1

The official source for the Java standard library is the standard Java API documentation.

And one notable source for design patterns is the book Design Patterns: Elements of Reusable Object-Oriented Software.

To begin with, when you look at the options, the question (as quoted in your post) is badly formulated: "Which of the following classes in java library"...

Only the fourth option (Proxy) can refer to a class in the Java standard library. The other three are NOT classes in the Java library (they are either interfaces or they don't even exist as any type be it interface, class or enum). Also there are two Proxy classes in the Java library and this question doesn't specify which one. Either the question was misundertood (and incorrectly rephrased) or you may want to mention this inconsistency to whoever composed it.

So the correct option may either be the fourth option, or may be none of them depending on which Proxy we consider.

So let's take each option:

  1. Singleton: There is no class (or even type) with such name in the JDK standard library. Also, Singleton is a design pattern. So not this option.

  2. Observer: There is an interface (java.util.Observer) in the JDK, not a class. Also, Observer is a design pattern. So neither this one.

  3. Iterator: same as Observer. It's an interface and refers to a design pattern.

  4. Proxy: There are two classes in the JDK. java.net.Proxy represents a network proxy setting, hardly ever related to the proxy design pattern. If this is the proxy that's referred to, then the option is probably the correct one.

    If, on the other hand, we consider java.lang.reflect.Proxy which implements a dynamic proxy class that delegates method calls to other objects through an invocation handler, then this option would refer to a design pattern, making it not a correct option.

Answers 2

Some claim the Singleton is an anti-pattern because it holds a global state. The others are definitely patterns.

Answers 3

Except Singleton, all the options are types (not Classes but types atleast) in Java. Considering some claims by others of "Singleton being anti-pattern", the first option seems to be correct answer to the question (if question was not messed up).

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment