• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton & Factory  RSS feed

 
Allan Cheong
Ranch Hand
Posts: 71
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am confused whether an object could still be considered a Singleton when it's constructor has default access but the factory constructed it has a private static member of it which tests if the member is null, the factory will instantiate the new object. Otherwise, return the same reference of the private static member. For instance:



Does Object still considered to be Singleton? Note that the getObject method is synchronized.

Regards,
Allan
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this an academic question?

In practice, a Singleton is enforced mostly by convention. i.e. don't use it otherwise.

And of course, Use of the Singleton Pattern is considered harmful.
 
Allan Cheong
Ranch Hand
Posts: 71
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you say that academically object is not a Singleton but practically it is?
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@allan, I don't understand your question.


It is very hard to technically enforce the Singleton pattern. I'm not sure that it is even possible in some spaces, such as a Servlet container. But much more important to me, not to enforce that its really a singleton, but simply to not use the Singleton pattern at all.

From an software engineering perspective, I think that the Singleton Pattern is a disaster on many levels. It should be avoided.

Once you try to avoid it, you don't have to worry about technical issues about whether or not its technically accessible. Rather you simply say "don't do that"
and agree not to use the Singleton Pattern.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allan Cheong wrote:
Does Object still considered to be Singleton? Note that the getObject method is synchronized.


same kind of question asked manytime Here. please search in this forum
 
Allan Cheong
Ranch Hand
Posts: 71
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, from what I can see that is for instance:



Do you agree that the above is a Singleton? Second example which includes a Factory -



I know that in the second example, it would be disasterous if a programmer constructs AmIStillSingleton without using the Factory class. But still, AmIStillSingleton a Singleton?

@Pat: I am doing SCJD and from the posts I read in the SCJD forum, everyone is using the Singleton pattern. I am just following the herd . But I am just wondering if the object is constructed like the example above (example 2), does it still consider a Singleton?

@seetharaman: Oops sorry. I will search the forum again.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allan Cheong wrote:@Pat: I am doing SCJD and from the posts I read in the SCJD forum, everyone is using the Singleton pattern.


If you need to understand it to pass some certification, please don't listen to me. If you are trying to do professional software development, please do not use the Singletton Pattern.

I'll step out of this.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!