difference between class with static method and singleton pattern
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ashish_Kulkarni
Posted On:   Monday, February 10, 2003 09:01 AM

Hi, If I have a class with a static synchronized method and a class with singleton pattern doing the same function, wont the 2 be same, what may be the performance issue, what should we use in programs, The method i want to use is synchronized, so that no 2 classes can access it a time, and hence create some weird results. This is what my classes will look Class with static method public class MyString { public static synchronized String removeEscapeChar(String inputString) { // my logic here } } Singleto   More>>

Hi,


If I have a class with a static synchronized method
and a class with singleton pattern doing the same
function,

wont the 2 be same, what may be the
performance issue, what should we use in programs,


The method i want to use is synchronized, so that no 2
classes can access it a time, and hence create some
weird results.



This is what my classes will look Class with static method


public class MyString


{


public static synchronized String
removeEscapeChar(String inputString)


{


// my logic here


}


}


Singleton pattern class


public class MyString


{


private static MyString instance;


public static MapsContextData getInstance()


{


return instance;


}


public String removeEscapeChar(String inputString)


{


// my logic here


}



}



=====


A$HI$H

   <<Less

Re: difference between class with static method and singleton pattern

Posted By:   Bryan_Dollery  
Posted On:   Sunday, March 2, 2003 03:27 PM

First, there is probably no such thing as a singleton in Java.


Second, the concept of a singleton is that a single instance of a class should exist -- the concept of class-level methods is that no instance should exist -- these two things seem quite different to me.


Cheers,>br>Bryan

Re: difference between class with static method and singleton pattern

Posted By:   Vishal_Gandhe  
Posted On:   Tuesday, February 11, 2003 04:13 AM

I think using static methods has some disadvantages as compared to singleton pattern, some of those are,

- You have to hard code the class name with all the method calls that may reduce ur abstraction.

- You can not use static methods to provide the implementation of the interfaces.



VG

Re: difference between class with static method and singleton pattern

Posted By:   Benoit_Quintin  
Posted On:   Monday, February 10, 2003 12:07 PM

Well, first a singleton usually garantees only one copy of the class exists within one VM.

A static method implies that an instance of the class is not needed when invoking said method.

Therefore, a singleton could contain a synchronized static method. Synchronizing the method call of a static method usually insures the method will be treated as an atomic process. Sync'ing the singleton can mean locking the whole singleton object or locking one method of the object. Which implies that the singleton could be modified by other methods, or not at all. It all depends on your implementation, really.

But fundamentally, a Singleton is a (rather) frequently used pattern while a static method is a part of the Java language per se.

HTH

About | Sitemap | Contact