• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Singleton Question

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I ahve one doubt in Sigleton class.

For the Singleton class whether following is Mandatory:
1. Constructor should be private.
2. The object should be static.
(i.e., static classname ref = new classname(); )
3. Use static method to get the instance.

a.) Whether we can achieve the singleton class only by satisfying the above three conditions. There is any way to achieve in the same in different way.

b.) Why can't we use all the variables and method be private rather than using the constructor as private for the singleton class.

c.) Whether inheritance is possible in Singleton class(if constructor is not marked as private)
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The singleton class has to have all it's contructors private. If you do not make them private then any client code can instantiate your singleton class. Which is something that you want to avoid.

The variable of your singleton class must also be static because the getInstance() method is static, and the getInstance() method is static because you can't create an instance of the singleton. (ie Singleton x = new Singleton() )

See singleton example below. You'll see that you have to make the construstor private so that no one else can instantiate your class. Therefore you have to make the getInstance() method public and static, so that any client doesn't need an instance of your class to exist to call the method, and that method returns the static instance of your singleton.

cheers
Darryl

public class Singleton {
private static Singleton instance;

private Singleton() {
}

public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (singleton == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
 
Darryl Nortje
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for not answering all your questions. Here hopefully is all the answers.

--> For the Singleton class whether following is Mandatory:
--> 1. Constructor should be private.
--> 2. The object should be static.
--> (i.e., static classname ref = new classname(); )
--> 3. Use static method to get the instance.

In my opinion they are mandatory. I haven't even thought about how else to do it, because that's really the point of using design patterns. You just implement the pattern, someone else has spent time on it finding the best way to do it, and I believe them ;-)

--> a.) Whether we can achieve the singleton class only by satisfying
--> the above three conditions. There is any way to achieve in the same
--> in different way.

You probably can, but as above why would you want to, this way works and it is a proven design.


--> b.) Why can't we use all the variables and method be private
--> rather than using the constructor as private for the singleton class.

Well if any client code can instantiate your class then it isn't a singleton. making the variables private doesn't stop someone from creating an instance of your class. It just stops them from gaining access to your variables. MyClass.variable = "test" is what making your variables private avoids.

--> c.) Whether inheritance is possible in Singleton class(if
--> constructor is not marked as private)

Inheritance is possible yes. Whether your constructor is private or not. You only inherit your super classes public and protected methods, not your super classes construstors. And if your super class is not a singleton, then anyone can instantiate your super class only, not your singleton. Your singleton will have it's own instance of its super class.


Hope this helps.

cheers
Darryl
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Singleton pattern has a couple things going on. One is to control the number of instances. In a small note the pattern actually allows more than one - the control part is important. Another part is to provide a way to get the instance. The static getInstance() technique shown above is fine. The double check for null is overly tricky and not guaranteed to work in all situations. I'd simplify lazy creation a bit to just:

or skip the "lazy" part for even simpler

Be aware of another pattern not in the famous books that is sometimes called Just Make One. If you've done Servlet programming you might have have noticed that your servlets have public constructors but you never do a new on them. They are singletons without the control aspect, which makes them Just Make One. You are just expected to know, or discover quickly, that if you make your own instances they don't do anything useful.
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Inheritance is possible yes. Whether your constructor is private or not. You only inherit your super classes public and protected methods, not your super classes construstors. And if your super class is not a singleton, then anyone can instantiate your super class only, not your singleton. Your singleton will have it's own instance of its super class.



Correct me if I am wrong, inheritance is possible in singleton class, only if the singleton class should not be a super class. A singleton class can inherit from the super class public and protected methods.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, it helps if you override the clone method in your Singleton class.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Inheritance is possible yes. Whether your constructor is private or not.

Perhaps I'm missing something, but if a class only has private constructors, how are you going to extend it? Can you provide a compileable example of a class that extends the following?
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Correct me if I am wrong, inheritance is possible in singleton class, only if the singleton class should not be a super class.

Inheritance should not be allowed of a Singleton. It wouldn't be a Singleton if clients could instantiate a bunch of new subtypes.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The Singleton pattern has several advantages over the "static class" pattern. First, a singleton can extend classes and implement interfaces, while a static class cannot (well, it can extend classes, but it does not inherit their instance members). A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded. A sinlgeton class can be extended and it's methods overidden.
Perhaps the most important advantage, though, is that singletons can be handled polymorphically without forcing their users to assume that there is only one instance. For instance, assume you have a Configuration class that holds some global configs. Methods that use this configuration information may be defined as:

public void doSomething(Configuration config) {...}

When you start writing your system you may have only one global instance, so you make Configuration a singleton. But at some later point you may want to support more than one configuration set. Maybe you'd want to allow the user to load a Configuration object from and external file or or programmaticaly create his own (this happened in my code several times). Provided that the classes or methods that use a configuration object allow the user to pass his own instance, most of your code need not be aware of the "global" nature of the class and is therefore more flexible.



Whether inheritance is possible in Singleton. If so, please give example codings. The above quotes is taken from theserverside.com in the answer section for the question "How singleton class is differ from static class?"
 
Darryl Nortje
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I meant by inheritance is possible with singleton is that the singleton can inherit from it's super class. Because your constructor is private you cannot create a class that extends your singleton. Because of the implicit call to super() in your constructor.

sorry for the misunderstanding.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are other mechanisms to control a singleton besides a private constructor. See if this makes sense:

Note the packages and visibility modifiers. The Tester class cannot see the PrivateOne class so it can never create one. Try importing and creating one to see. The Accessor can create any implementation of PublicInterface it likes. You can use inheritance freely from PrivateOne or anything else since all you have to do is implement a simple interface!

(I did a little post-testing editing to clean up a couple signatures. Lemme know if it doesn't really compile!)
[ July 28, 2005: Message edited by: Stan James ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic