This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Singletone Pattern

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends,

A class designed as Singletone pattern should be final, am i right or not?.


Regards
Kasimurugan R
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite. Making it final would make it safer in cases where other people are going to use your class and which may try to subclass it. Still, i would make it final just in case, but the trick is to have only one private no-arg constructor. Have a static method which checks the static reference to the object (itself) to see if it has been instatiated already or not. Make sure you handle thread-safety issues (ie. you might want to synchronize the static method), and watch out for clients which try to clone the singleton (you can implement your own clone() method in the singleton class which simply throws and exception when called).

Regards.
[ September 13, 2006: Message edited by: Marcelo Ortega ]
 
KasiMurugan Ramasamy
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friend,

But if we try to extend, what will happen. Gues !

Compiler will report as error.

Since the subclass's constructor(may be default constructor) try to access superclass's constructor. In the superclass we declared as private constructor. So subclass can't access it.

Am i right?.

That is the reason, i mentioned a class designed as singledtone pattern implicitly Final Class.

Am i right?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GoF goes on at length about subclassing and ways to choose which concrete class to use. If the private constructor prevents extension in Java, that's a problem with the implementation, not a rule of the pattern.

GoF says the class must be written so that only one instance can be created. If we fudge that rule and move the responsibility for singleness to a factory or registry we can lose the private constructor.
 
Marcelo Ortega
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that we put in a private constructor means that it cannot be subclasses, but by declaring it as final, we make our intention more explicit and allows the compiler to apply performance optimizations.

Regards.
 
KasiMurugan Ramasamy
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai

Design Pattern is a rule. As per the singletone pattern rule, we have to allow only instance in a JVM for a Class.

We can design anyway, but we have to mind the Singletone Pattern rule.

So private constructor is a one way to design singletone pattern.

According to Stan James, i understand that we can design singlepattern in some other way like factory, registry or anyway with allowing one instance per class.

So my conclusion is Singletone pattern need not be a Final Class.

Thanks for Stan James.
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic