The Artima Developer Community
Sponsored Link

Java Answers Forum
Singleton vs Class with static methods

6 replies on 1 page. Most recent reply: May 19, 2003 12:14 AM by avadhut

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 6 replies on 1 page
The Phoenix

Posts: 6
Nickname: phoenixb
Registered: May, 2003

Singleton vs Class with static methods Posted: May 13, 2003 4:18 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 <b>an actual or real scenario/example </b> which illustrates point 3.


Jaycee

Posts: 26
Nickname: jaycee
Registered: Apr, 2003

Re: Singleton vs Class with static methods Posted: May 13, 2003 12:01 PM
Reply to this message Reply
public class DataSource{
	private DataSource _data_source;
	
	private DataSource{}
 
	public static DataSource getDataSource(){
		//to make this threadsafe, synchronize should be added
		if(_data_source==null){
			//assuming rdbms has been set in the System properties.
			String rdbms = System.getProperty("rdbms");
 
			if(rdbms.equals("oracle"){
				_data_source = new OracleDataSource();
			}else if(rdbms.equals("mySQL"){
				_data_source = new MySQLDataSource();
			}else if(rdbms.equals("MSSQLServer"){
				_data_source = new MSSQLServerDataSource();
			}else{
				throw new IllegalStateException("rdbms was not found in system properties");
			}
		}
 
		return _data_source;
	}
}

The Phoenix

Posts: 6
Nickname: phoenixb
Registered: May, 2003

Re: Singleton vs Class with static methods Posted: May 13, 2003 11:55 PM
Reply to this message Reply
Jaycee,
Isn't this an illustration of Point 2? Well, let me phrase my question this way:
1.If i don't need to subclass my Singleton, and
2. if i don't need more than 1 instance,
why shouldn't i use a class with static methods instead?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Singleton vs Class with static methods Posted: May 14, 2003 1:31 AM
Reply to this message Reply
Having a singleton is more flexible.

You can pass the instance to methods, so it isn't treated as a global and users are not so tightly coupled to it, especially if it implements an interface that could potentially be implemented by other classes (singletons or not). Things you do with objects will be simpler and cleaner (serialization, comparison, aggregation, etc.).

At some later time, if you decide to make it multi-instance, that will be a relativly easy change to implement.

Singletons make sense when you think of the thing as a object of which you only expect to have one instance. Static methods make sense when you have a group of methods which are really all stand-alone, but can be grouped in a class for clarity (like math or the like). In this case the class may be a zero-ton and really only serves as a namespace or package. (This is aside from the case of having a static helper method or two in a class which can be instantiated).

The Phoenix

Posts: 6
Nickname: phoenixb
Registered: May, 2003

Re: Singleton vs Class with static methods Posted: May 14, 2003 6:41 AM
Reply to this message Reply
> You can pass the instance to methods, so it isn't treated
> as a global and users are not so tightly coupled to it,
> especially if it implements an interface that could
> potentially be implemented by other classes (singletons or
> not). Things you do with objects will be simpler and
> cleaner (serialization, comparison, aggregation, etc.).
1. What is the need for passing a reference to a Singleton? By definition, the only way to obtain a reference should be by calling a typical "getInstance()" method. Within a single JVM, we shouldn't be passing references of Singletons to methods.
2. If you are talking about passing a reference of a Singleton to a remote JVM, then your point makes sense although i don't see any motivation for that. Even assuming there is a need, the difference comes into play ONLY when there is modifiable state in the Singleton. A Singleton reference can be passed with its state intact whereas it doesn't make sense in passing a .Class object :)


> At some later time, if you decide to make it
> multi-instance, that will be a relativly easy change to
> implement.
Agreed

> Singletons make sense when you think of the thing as a
> object of which you only expect to have one instance.
> Static methods make sense when you have a group of
> f methods which are really all stand-alone, but can be
> grouped in a class for clarity (like math or the like).
> In this case the class may be a zero-ton and really only
> y serves as a namespace or package. (This is aside from
> the case of having a static helper method or two in a
> class which can be instantiated).
Maybe you have a point but i'm not convinced.

Jaycee

Posts: 26
Nickname: jaycee
Registered: Apr, 2003

Re: Singleton vs Class with static methods Posted: May 14, 2003 9:14 AM
Reply to this message Reply
>1.If i don't need to subclass my Singleton, and
>2. if i don't need more than 1 instance,
>why shouldn't i use a class with static methods instead?

If that's the case, I can't think of a good reason not to use static methods. I think the aversion to using static methods stems from the fact that once you go this route, you've lost the flexibility to change your mind on subclassing or instance pooling in the future.

avadhut

Posts: 1
Nickname: ava
Registered: May, 2003

Re: Singleton vs Class with static methods Posted: May 19, 2003 12:14 AM
Reply to this message Reply
Hi ,
This is about the singleton vs staticmethods in class
What I feel is that if a class has any object level variables which are supposed to get loaded once and then they should no be changed easily by any other classes which are using this class....then its better to make it singleton , while if it dont have any object level varibles then then better to make it static methods

Pl let me know ur views as currently i m also a bit confused

...Ava

Flat View: This topic has 6 replies on 1 page
Topic: java exe file? Previous Topic   Next Topic Topic: JAKARTA  STRUTS !!!!!!!!!!!!!!!!!!

Sponsored Links



Google
  Web Artima.com   

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