Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click NOW to join Planet IT!
Click NOW to join Planet IT!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Singleton Pattern

The singleton pattern is a design pattern used to ensure that only one instance of a class is instantiated. This single instance is shared by any classes requiring its services.

The singleton pattern can be implemented in Java by using a protected, or private, constructor for a class and a public access method to obtain a handle to the single instance of the class. This is demonstrated with the following simple example:

public class MySingleTon extends Object {

    /**
     * The reference to the one and only instance
     * of the mySingleTon class.
     **/
    private static MySingleTon mySingleTon = null;

    /**
     * Protected constructor ensures that this class,
     * or subclasses, are the only entities
     * that can create instances of the class.
     **/
    protected MySingleTon() {
    }

    /**
     * Public access method.
     * Lazily create the shared instance of this class.
     * @return the shared instance of this class.
     **/
    public static MySingleTon getMySingleTon() {
        if( mySingleTon == null ) {
            mySingleTon = new MySingleTon();
        }

        return( mySingleTon );
    }

    // Public service methods for this class
    // would follow.
    //
    public void doSomething() {
    }
}

In this example, the constructor method is protected. This prevents multiple instances of the class from being created, but still allows the class to be extended. If the class were final, the constructor would be private. A static public access method is provided that allows clients to retrieve a handle to the shared instance of the class. In this example, the instance is lazily evaluated. The shared instance will not be created until it is requested. If the shared instance must be created at all times, the static initializer method called when the class is loaded could be used to create the shared instance. As in any other class, public methods are provided to operate on the singleton instance.

Clients use the singleton instance by first calling the static access method to obtain the reference to the instance. Then methods can be called on that instance, just as if it had been created with the new operator. The following example shows how a client may use the MySingleTon class:

MySingleTon mySingleTon = MySingleTon.getMySingleTon();
//
// Call methods on mySingleTon
//
mySingleTon.doSomething();

The JFC contains many examples of singleton classes. Most of them are service-type classes. One such example is the ToolTipManager class. The single instance is retrieved via the sharedInstance static method. The ToolTipManager class is discussed in Chapter 25, “ToolTips and Debug Graphics.”

Summary

This chapter introduced the architecture on which the JFC is built. The JFC is built on a modified Model View Controller architecture. The view and controllers are combined into a single object in the JFC. This simplification requires less messaging between objects. Specifically, the views do not need to message controllers, who in turn message the model in response to user gestures. Instead, the view messages the model directly. This simplification reduces messaging at the cost of reducing flexibility in the design and reuse of controller objects.

The factory and singleton design patterns were introduced in this chapter. These design patterns are used throughout the JFC. Having an understanding of these patterns will enable you to obtain a deeper understanding of the JFC, and it will allow your code to build on the JFC architecture and not simply use the toolkit.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.