• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Allow to create only ONE instance of a class

 
Vivek Chaitanya
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

This is the Brain Barbell from HFJ, Page#280.Chapter#10, Numbers and Statics.

What if you want to write a class in such a way that only ONE instance of it can be created, and anyone who wants to use an instance of the class will always use that one, single instance?

Someone please enlighten me how to achieve this.

Thank you.

Until next time,

Chaitanya
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vivek & welcome to the forum.
Originally posted by Vivek Chaitanya:
Hi All

This is the Brain Barbell from HFJ, Page#280.Chapter#10, Numbers and Statics.

What if you want to write a class in such a way that only ONE instance of it can be created, and anyone who wants to use an instance of the class will always use that one, single instance?

Someone please enlighten me how to achieve this.

Thank you.

Until next time,

Chaitanya


The Math() class is in this category. Take a look at it to get some ideas. Also, I read this, but forget if it was HFJ or K&B, *think* (and look for topics on) private constructors.

Since this is a brain buster, I'm not going to explicitly tell .

Aloha,
Doug

-- Nothing is impossible if I'mPossible
 
chander shekhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you really want to have one and only one instance of that class you need to do just two things.

1. Make the constructor(s) of that class private.
2. Now to create an instance of that class you need to have a public static method inj that class. Also take a reffernce variable of the same class.
This method say getInstance() will check the above said reffernce variable and if found null, call the private constructor and assign the object to the reffernce variable. this method then return the instance. If it find the reffernce variable to be not null Then it just return the old object.

i hope it will help you.

-- Shekhar
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chander shekhar:
This method say getInstance() will check the above said reffernce variable and if found null, call the private constructor and assign the object to the reffernce variable. this method then return the instance. If it find the reffernce variable to be not null Then it just return the old object.

Note that this does not guarantee that only a single instance is created in a multi-threaded environment (i.e., it is not thread-safe).
 
chander shekhar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes You are right.
I missed out that thing. In that case we need to synchronize the method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic