Design Patterns Interview Questions
Design Patterns Interview Questions
The programmer would access the single instance of this class by writing something similar
to
Singleton.getInstance().someMethod()
or similar to
Singleton s = Singleton.getInstance();
s.method1();
...
s.method2();
...
For a more complete discussion of the Singleton pattern, see the chapter “Singleton” in the
book Design Patterns: Elements of Reusable Object-Oriented Software by the “Gang of
Four” (Addison-Wesley, 1995), or the chapter “Singleton” in the book Patterns in Java,
Volume 1 by Mark Grand (John Wiley & Sons, 1998). For information about variations on
the Singleton Pattern, see the chapter entitled “To Kill a Singleton” in the book Pattern
Hatching: Design Patterns Applied by John Vlissides or the article “Implementing the
Singleton Pattern in Java” by Rod Waldhoff.
6) Calendar is an abstract class. The getInstance() method
tries to instantiate GregorianCalendar() i.e., parent
instantiating a derived class. This looks Non-OO? Ex:
Calendar a=Calendar.getInstance(); Can somebody explain
why is it so?
The Calender class is an abstact class, true, however,the point you missed is that the
getInstance() returns the " Calendar using the default timezone and locale. " , in your case,
the GregorianCalender a class that IS a Calender (a Suzuki IS a car, a 747 IS a plane..the
standard OO terminology. So what you get is a class that does some specialized work based
on the default locale. Other methods
return Calenders for specific timezones and locales. The closest parallel is possibly the
Factory Method design pattern.
package com.jgk.patterns.singleton;
public class JGKSingleton {
NOTE: The 2nd check for if (instance_ == null) is needed to avoid making another unnecessary
construct.
Don't let this byte you! ;-)
18) How and where did the concept of design patterns get
started?
Work on patterns has been influenced by the works of Christopher Alexander who published
on topics related to urban planning and building architecture in the late 1970s. The history
of patterns for software design began in the late 1980s and reached an important milestone
with the publishing of the first book fully dedicated to this subject by the "Gang of Four",
Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Design Patterns - Elements
of Reusable Object-Oriented Software). In conjunction, the emergence of object-oriented
software development fostered the work on other topics related to design patterns such as
application frameworks, analysis patterns, language idioms, and so on.
Note that Cloneable is an empty interface! It merely acts as a tag to state that you really
want instance of the class to be cloned. If you don't implement Cloneable, the super.clone()
implementation will throw a CloneNotSupportedException.
The Object implementation of clone() performs a shallow copy of the object in question.
That is, it copies the values of the fields in the object, but not any actual objects that may
be pointed to. In other words, the new object will point to the same objects the old object
pointed to.
As an example of using the cloning:
This example is pretty trivial, but the real power comes when you don't know what you're
actually cloning.
For example, suppose you define an interface that represents a customer:
You might have several different implementations of this interface, possibly storing data in a
file, database, or using EJB Entity beans. If a shallow copy of the data is sufficient to
represent a copy of the object, Java's clone() method works great.
You might have a method that needs to make a copy of the data to store it in a Hashtable,
for example:
Note that this method knows nothing about what type of customer we're getting. This
pattern will work for any actual type of Customer, no matter how the data is stored. For
example:
manager.storeCustomer(c1);
manager.storeCustomer(c2);
manager.storeCustomer(c3);
class PieceOfCode {
public Object myMethod() {}
}
class EJBImpl ... {
PieceOfCode poc = new PieceOfCode();
public Object myMethod() {
return poc.myMethod();
}
}
This should not be a violation of EJB specs, since EJBs can use simple java classes for their
use. Think about Dependant Objects and so on.