Global Access Point – SINGLETON Design Pattern

with 6 comments

*Click on image to view it better


                          Design Patterns provides better recurring solutions for commonly occurring problems says Gang Of Four and SINGLETON is one such DESIGN PATTERNS.

There must be only one instance to a class and that instance must be the MAIN IMPORTANT GLOBAL POINT OF ACCESS to all other related Classes.

A Singleton Class must have 3 important things,
1. A private static Instance member
2. A private constructor
3. public static getInstance() – to get Instance Member specified in POINT 1

Before entering into Singleton, let us get into a quick discussion on CLASS ACCESS.

Access specifiers in Java are a real gift to the Java Developers because they are much like a CONTROL SYSTEM of our project.It is in our hands to decide what to expose and what not to expose to the world by using the right specifier at the right place.It is generally a good practice to make fields in the class as much PRIVATE as possible, if that field’s need is not outside the class.

Here l also want you to notice that a Class can be either PUBLIC OR DEFAULT and cannot be PRIVATE OR PROTECTED

  • Public Class can be accessed by any class from any Package
  • Default class can be accessed by the classes within the same Package.

Are you with me??? I don’t want to bore you anymore…Let’s go into a SINGLETON example now.


Consider , You have a MUSIC STORE in your IPod– That has many categories(as of now in program I have considered Devotional Songs and Pop Songs).


PopSongs and DevotionalSongs are two classes that implement the Interface IMusicAction .

Consider the interface has a method Play(), that is implemented in both the classes PopSongs and DevotionalSongs, but performing different playAction.


Hold On…! Have you noticed that both the classes are of DEFAULT type … and NOT Public.
Can you reason out why……????

Tat’s bacause I do not want these classes to be accessed out anywhere out of this package. i.e., It’s access is only within that package(com.aishu.practice.access).

Now let’s create the MUSICSTORE.We are going to make it Singleton – which means a SINGLE STORE(a SINGLE INSTANCE) having different song types(holding TONS OF INSTANCES)


Follow these steps,

  1. Create private static Instance member private static final MusicStore musicStoreInstance = new MusicStore();
  2. Create a CONSTRUCTOR and make it PRIVATE– Why PRIVATE ????

Make it Private because I don’t want to create the OBJECT OF MY CLASS anywhere else excepting ONLY INSIDE MY SAME CLASS ITSELF.

  1. Create public static method gets the Instance Member specified in Point 1
  2. Create Instances for the song types, Devotional and Pop Songs and PLAY ON

Here MusicStore class is PUBLIC. So no restrictions..let’s try calling it from a class outside this package and create instance of it to get our desired music played on…


In the main method , you will not be able to create new Object for MUSICSTORE class.You can only get its instance.How many ever times you try creating a new instance, the Instance still remains the same SINGLE INSTANCE (Marked in Pink).Now after getting this singleInstance, you can get any type of song Played On .


Don’t you think Singleton is much more related to SUBSET FUNCTIONS we learnt in Mathematics by drawing Venn Diagram during our school days….untitled




Okay…….In my next blog I will tell you, how Multi threads work on a Singleton Class.

Written by thangaveluaishwarya

May 17, 2013 at 1:00 PM

6 Responses

Subscribe to comments with RSS.

  1. I like the Public classes pollution explanation, good start read more on Encapsulation and packaging principles.

    Arul Prasad

    May 17, 2013 at 4:38 PM

  2. Interesting and detailed! Can post more code?

    Srivesh Khanna

    May 18, 2013 at 8:49 AM

  3. Good one. I like the PUBLIC and DEFAULT explanation you did as part of this Singleton. Go ahead.

  4. helpful and understood what is on multi threaded

    Java Guy

    May 18, 2013 at 9:37 PM

  5. best explanation till date. not many talk about scopes.


    June 21, 2013 at 1:27 PM

  6. The single best explanation I’ve read in a long time.


    November 10, 2017 at 2:54 AM

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: