Q5. What is an example of a design pattern?

Following the lead of the “Gang of Four” (GoF), design pattern descriptions usually contain multiple sections including
• Intent
• Motivation
• Applicability
• Structure
• Participants
• Collaborations
• Consequences
• Implementation
• Sample Code
• Known Uses
• Related Patterns
A complete discussion of even a small pattern is beyond the scope of a simple FAQ entry, but it is possible to get the idea by examining an abbreviated discussion of one of the simplest and most easily understood patterns. Consider the Singleton pattern, whose intent reads as follows:
Intent: Ensure that a class has one instance, and provide a global point of access to it. Almost every programmer has encountered this problem and formulated an approach for solving it in a general way – some solutions are better than others. The solution offered by the GoF would look something like the following when coded in Java.

public class Singleton
{
private static Singleton instance = null;
public static Singleton getInstance()
{
if (instance == null)
instance = new Singleton();
return instance;
}
protected Singleton() { ... }
// possibly another constructor form
public void someMethod() { ... }
//... other methods
}
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.

Q6. 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
public static synchronized Calendar getInstance(TimeZone zone,Locale aLocale)
public static synchronized Calendar getInstance(TimeZone zone)
return Calenders for specific timezones and locales. The closest parallel is possibly the Factory Method design pattern.

Q7. What major patterns do the Java APIs utilize?

Design patterns are used and supported extensively throughout the Java APIs. Here are some examples:

• The Model-View-Controller design pattern is used extensively throughout the Swing
API.
• The getInstance() method in java.util.Calendar is an example of a simple form of
the Factory Method design pattern.
• The classes java.lang.System and java.sql.DriverManager are examples of the
Singleton pattern, although they are not implemented using the approach
recommended in the GoF book but with static methods.
• The Prototype pattern is supported in Java through the clone() method defined in
class Object and the use of java.lang.Cloneable interface to grant permission for
cloning.
• The Java Swing classes support the Command pattern by providing an Action
interface and an AbstractAction class.
• The Java 1.1 event model is based on the observer pattern. In addition, the
interface java.util.Observable and the class java.util.Observer provide support for
this pattern.
• The Adapter pattern is used extensively by the adapter classes in java.awt.event.
• The Proxy pattern is used extensively in the implementation of Java's Remote
Method Invocation (RMI) and Interface Definition Language (IDL) features.
• The structure of Component and Container classes in java.awt provide a good
example of the Composite pattern.
• The Bridge pattern can be found in the separation of the components in java.awt
(e.g., Button and List), and their counterparts in java.awt.peer.

Q8. How can I make sure at most one instance of my class is ever created?

This is an instance where the Singleton design pattern would be used. You need to make the constructor private (so nobody can create an instance) and provide a static method to get the sole instance, where the first time the instance is retrieved it is created:

public class Mine {
private static Mine singleton;
private Mine() {
}
public static synchronized Mine getInstance() {
if (singleton == null) {
singleton = new Mine();
}
return singleton;
}
// other stuff...
}