Posted in Core Java, Design Pattern, Object Oriented Programming

Singleton Pattern

A Singletone class;

Only one time in JVM per class loader would be instantiated. Same instance would be use for repeated calls.

public class OnlyOne {

            private static OnlyOne one = new OnlyOne();

            // private constructor, so this class cannot be instantiated from outside and prevents

            //subclassing.

private OnlyOne();

//a global point of access

public static OnlyOne getInstance() {

            return one;

}

}

To use it:

OnlyOne myOne = OnlyOne.getInstance();

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s