The Artima Developer Community
Sponsored Link

Design Forum
Singleton vs Class with static methods

14 replies on 1 page. Most recent reply: Mar 31, 2008 10:47 AM by jigyaasa jigyaasa

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 14 replies on 1 page
The Phoenix

Posts: 6
Nickname: phoenixb
Registered: May, 2003

Singleton vs Class with static methods Posted: May 13, 2003 5:37 AM
Reply to this message Reply
Advertisement
This is about the trade-offs between using a class as a Singleton as against having a class with all Static methods. As it is oft-repeated, using a Singleton approach would have the following benefits:

1. You can control the number of instances. ie, it's not actually *Single*ton.
2. You can sub-class a Singleton while it doesn't make sense to sub-class a class with only static methods.
3. You can control the creation of the actual instance whereas a static approach wouldn't give that flexibility.

Now, leaving aside the first two points for all practical purposes (we are talking about <i>Singletons</i> only), can anyone give an actual or real scenario/example which illustrates point 3.


vinayak

Posts: 2
Nickname: vinban
Registered: May, 2003

Re: Singleton vs Class with static methods Posted: May 16, 2003 5:20 AM
Reply to this message Reply
see If we have a class with static method , I can have any number of object right,
But if i make constructor as private i can not make a class directly. i have to use a static method of that class to create the object of that class ,ight,
when i call the static method of that class i can keep track of the instance of the class.

The Phoenix

Posts: 6
Nickname: phoenixb
Registered: May, 2003

Re: Singleton vs Class with static methods Posted: May 16, 2003 6:36 AM
Reply to this message Reply
> see If we have a class with static method , I can have any
> number of object right,
> But if i make constructor as private i can not make a
> class directly. i have to use a static method of that
> class to create the object of that class ,ight,
> when i call the static method of that class i can keep
> track of the instance of the class.
Vinayak,
Please read the question properly.

vinayak

Posts: 2
Nickname: vinban
Registered: May, 2003

Re: Singleton vs Class with static methods Posted: May 20, 2003 1:36 AM
Reply to this message Reply
hi The Phoenix

Sorry , i think i did not read the question properly.
But i think Point 3 is similary to point 1.

Kamal

Posts: 3
Nickname: kamal079
Registered: May, 2003

Re: Singleton vs Class with static methods Posted: May 23, 2003 10:56 AM
Reply to this message Reply
Hi,
Check with the complete articles found at the following link. It might answer your question

http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html

regards
Kamal

zenykx

Posts: 69
Nickname: zenykx
Registered: May, 2003

Re: Singleton vs Class with static methods Posted: May 25, 2003 1:45 PM
Reply to this message Reply
What if you want your singleton class to be initialized to some initial values ? With static method approach you are going to provide the arguments for each call ...

---
There are only solutions !
zenykx

Adam McIntosh

Posts: 2
Nickname: adammc
Registered: Jul, 2003

Re: Singleton vs Class with static methods Posted: Jul 18, 2003 5:28 AM
Reply to this message Reply
A Singleton will return you an object pointer or reference, that you can pass around.
With static methods all you are doing is providing a namespace to encapsulate your functions. You don't actually have an object.
So if you want objects, and you want polymorphism, then Singleton's your baby.

Rob Ong

Posts: 1
Nickname: insane
Registered: Aug, 2003

Re: Singleton vs Class with static methods Posted: Aug 7, 2003 8:21 PM
Reply to this message Reply
Which among those two is the fastest?

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Singleton vs Class with static methods Posted: Aug 23, 2003 4:42 PM
Reply to this message Reply
Since you are expecting a practical scenario to understand Point 3 in your question let me try to explain some of them.

The first example I can think of is the use of singleton EJB Home Factory Design Pattern. Since before creating a remote interface you will always need a home interface to create the remote interface. Moreover every EJB will have only ONE home Interface. Therefore a Singleton can be used to guarantee that at any given time only one instance of a EJB is home interface exists.

The singleton will internally use some sort of Map (HashMap) to store all the already created Home interfaces and when you ask the Singleton class to create a new home interface it will check against the HashMap first and if it is available there it will return that else it will create one, store in the Map and then return the newly created Object.

Another example I can think of is say if you have a requirement in your Web Application that only a given user can only be logged in to the system only once. Basically the same username can not be used to login from different locations or browsers in the same machine.

Again Singleton will come to save you. Whenever the user logs in the first time the Singleton class will store the users session with his login name in a Map(HashMap) and then when the same user name is used to login again it will first check whether the session already exist for that user, if it does it will invalidate that session and create a new one and return that.

Hope this makes thinks clear for you

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Singleton vs Class with static methods Posted: Sep 1, 2003 3:35 PM
Reply to this message Reply
First remark :
A Singelton producing multiple instances is called a Factory...

Secondly,
the remark about initialized variables by an initializer that cannot be initialized with static methods on a class... is missing the point . There is in any case the possibility to use the static initializer, if necessary.

The most likely answer is that the static class will be faster than anything else ...
However the usage of a factory is IMHO the best option as the trade-off Speed vs Modularity is definitively in favor of the usage of a Factory. Make however sure that NO variable is &quot;shared&quot;, so there will be NO synchronization needed &amp; no thread accessing randomly variables...

\T,

Thomas Holaday

Posts: 1
Nickname: tlholaday
Registered: Feb, 2006

Re: Singleton vs Class with static methods Posted: Jun 9, 2006 5:33 AM
Reply to this message Reply
Real example of point 3:

The object is "expensive" to create because it contains a connection to a remote server. For many executions, the application's users do not require the remote server, so it is pointless to incur the expense of establishing the connection just in case. When the users do require the remote server, the Singleton is instantiated (once) and persists until the end of the execution.

ding junjie

Posts: 1
Nickname: dingjunjie
Registered: Nov, 2006

Re: Singleton vs Class with static methods Posted: Nov 12, 2006 5:30 AM
Reply to this message Reply
试一下汉语能不能发帖子(internationalization?)

Parag Shah

Posts: 24
Nickname: parags
Registered: Mar, 2003

Re: Singleton vs Class with static methods Posted: Nov 13, 2006 7:27 AM
Reply to this message Reply
I guess the main reason to have control over instantiation would be to allow for lazy initialization. However I have not come accross any application where early initialization in a Singleton was a performance bottleneck.

Now I'm thinking aloud here, but if the initialization of an attribute requires some complex computations, then the only way we can do it in a class with static methods is by a static block. Perhaps a constructer in a non-static Singleton might be a cleaner implementation.

This is not directly related to "control over instantiation", but if the Singleton impplements an interface then we cannot implement it as a collection of static methods.

--
Parag

pralay patoria

Posts: 1
Nickname: pralay123
Registered: Sep, 2007

Re: Singleton vs Class with static methods Posted: Sep 24, 2007 10:04 PM
Reply to this message Reply
> This is about the trade-offs between using a class as a
> Singleton as against having a class with all Static
> methods. As it is oft-repeated, using a Singleton approach
> would have the following benefits:
>
> 1. You can control the number of instances. ie, it's not
> actually *Single*ton.
> 2. You can sub-class a Singleton while it doesn't make
> sense to sub-class a class with only static methods.
> 3. You can control the creation of the actual instance
> whereas a static approach wouldn't give that flexibility.
>
> Now, leaving aside the first two points for all practical
> purposes (we are talking about <i>Singletons</i> only),
> can anyone give an actual or real scenario/example which
> illustrates point 3.

I think weather to use a class with all static methods or make it a singleton depends on the functionality that the class is providing.

I will give you a .Net example , if we have a class File which provides method to create, insert , delete , open etc. Then it is better to make all the exposed method as static instead of making it singleton. As File does need to have single instance logically. It is just providing functionality to create, delete or update. On the other hand if you have a Logger class , where you need that your application should use a single log , than single ton is the best way to implement that.

Advantages and Disadvantages of using singleton.

(1) Class with all methods as static can(in some scenario) be more efficient as there is single call to the method where as in singleton, there is one time overhead of instantiation and every time you have to call getinstance and then the method.
(2) Singleton can be designed in a more thread safe manner . While to create a thread safe class with all static method a developer has to be more cautious.
(3)Through Single ton class you have more control over creation of the instance , which is one of the important feature.

Pralay

jigyaasa jigyaasa

Posts: 1
Nickname: jigyaasa
Registered: Mar, 2008

Re: Singleton vs Class with static methods Posted: Mar 31, 2008 10:47 AM
Reply to this message Reply
Hi Pralay,

As summarized by you I do understand and agree to your points 1 & 3 but am a little confused on 2. Request all you there in the thread to help me out sort this doubt.
The point is about thread-safety of the static methods - what if in a particular scenario the class that has static methods has no state at all...is stateless. In this case too is there a need to handle thread safety. I am sure thread safety has to be handled if and only if there is a need which arises due to the read/write conflicts, write/write conflicts.

Flat View: This topic has 14 replies on 1 page
Topic: Invite: Software Architecture user research study - pls. help Previous Topic   Next Topic Topic: What do you wish the CIO understood about managing developers?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use