Hi,
Please give me a real time example for singleton pattern . Different threads accessing a shared file is singleton or not ? Since each thread access the same instance of the file not individual instances of their own .
Hi,
Please give me a real time example for singleton pattern . Different threads accessing a shared file is singleton or not ? Since each thread access the same instance of the file not individual instances of their own .
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:
.
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".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.)
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