example for Singleton pattern
Please give me a real time example for singleton pattern . Different threads accessing a shared fi开发者_如何学Pythonle is singleton or not ? Since each thread access the same instance of the file not individual instances of their own .
A "file" isn't a Java object (and java.io.File
definitely isn't a singleton). I wouldn't think of files on disk as a singleton either - they're just shared resources. In particular, it's not like there's only one file on disk :)
A more common example of the singleton pattern is configuration - or logging. For example, LogManager.getLogManager
returns "the" LogManager
, and you can't create new ones. Likewise you might have one common configuration object which can be accessed statically. (In a dependency injected system the configuration may well not be a singleton, however - instead each component is provided the bits of configuration they need so that they don't have to fetch a "common" one.)
Yes, but only if all threads access the same file, and you are using a custom implementation (not java.io.File
, perhaps a wrapper)
the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object
Singletons (being often a bad choice) are classes that can have only one instance throughout the whole program.
For example a SingletonConfigFile
might look like this. Have in mind that:
- it is for reading one file only. It would make sense for this to be a config file.
- if your class can be instantiated more than once, for different files, it is not singleton.
- don't use this code - it doesn't take into account concurrency problems which are a whole different area of discussion.
.
public SingletonConfigFile {
private static String filename = "config.xml";
private File file;
private static SingletonConfigFile instance;
private SingletonConfigFile() {
if (instance != null) {
throw new Error();
}
file = new File(filename);
}
public synchronized SingletonConfigFile getInstance() {
if (instance == null) {
instance = new SignletonConfigFile();
}
return instance
}
public String read() {
// delegate to the underlying java.io.File
}
}
But this example is on the edge of sense. Singletons are used in cases when there is only one object (as I stated above). For example it would make sense to have:
RingOfPower.getInstance()
- there is only one ring of power (Sauron's), and there can't exist more.Sun.getInstance()
- only one star called "sun".- all objects in the withing of your application that logically should exist only once - a registry, an application context, etc.
Singleton usually means having a class of which only one instance can exist.
A relaxation maybe being a class with a well-known system-wide single instance (in addition to other instances you may create).
java.io.File
is not a Singleton class.
Singleton is a design anti-pattern in which a class is given a private constructor and a special "getInstance()", "INSTANCE()", or "instance()" static function that is responsible for returning (and, possibly constructing, depending on whether you use lazy singletons or not) the sole object instance. Singletons often lead to dependency bloat while hiding dependencies and make it hard to mock out the singleton class during unit testing or to replace the singleton with something that isn't singleton, when it turns out that the assumption that the object should be singleton doesn't actually hold.
The solution to the singleton anti-pattern is to use something called "dependency injection". You might find this Google Techtalk, entitled Java on Guice, which explains dependency injection, enlightening.
There is also another form of singleton-ness, which is not related to the singleton anti-pattern... that is, if you happen to only create one instance of a class and pass that around, but you do not enforce singleton-ness using a static construction function, then you have made a class effectively singleton without locking yourself into the dependency or preventing the possibility of the class being not singleton later on. In the Google Techtalk that I have pointed out, the speakers mention this form of singleton-ness at the end of the talk.
In Java J2SE API - Two classes Calendar and Runtime are good examples of real time implementation of Singleton Pattern.
-Arun
Best real time example is to read property file data for that you need only single instance. please find following code to demonstrate example
public class SingleTonDesignPattern {
public static SingleTonDesignPattern singleTon = null;
public Properties priperty = null;
private SingleTonDesignPattern() {
}
public static SingleTonDesignPattern getSingleTon() {
return singleTon;
}
public Properties getPriperty() {
return priperty;
}
public void setPriperty(Properties priperty) {
this.priperty = priperty;
}
public static synchronized SingleTonDesignPattern getSingleTonObject() {
if(singleTon == null) {
singleTon = new SingleTonDesignPattern();
Properties properties1 = new Properties();
try {
properties1.load(new FileInputStream("c:/labels.properties"));
singleTon.setPriperty(properties1);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
return singleTon;
}
}
public class Singleton {
private static Singleton singleton = new Singleton( );
/* A private Constructor prevents any other
* class from instantiating.
*/
private Singleton(){ }
/* Static 'instance' method */
public static Singleton getInstance( ) {
return singleton;
}
/* Other methods protected by singleton-ness */
protected static void demoMethod( ) {
System.out.println("demoMethod for singleton");
}
}
精彩评论