tags:

views:

323

answers:

5

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 .

+1  A: 

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.
Bozho
then what about my file example above?
JavaUser
@JavaUser I updated my answer to clarify about your example
Bozho
@downvoter I'd appreciate your rationale.
Bozho
-1 Explanation: you build a complete argumentation about the possibility to wrap the access to a file into a singleton and I find this argumentation a bit wacky. File need to be considered as shared resource, you need to consider multithreading (explicitly mentioned in the question) and concurrency, etc. which your example does not really cover. See the other answers. The OP apparently appreciated your answer and accepted it, but I find this answer misleading and a bit of low quality compared to your other answers.
ewernli
@ewernli thanks for the explanation. Yes, the concurrency is not covered as it is a whole different area. My example is not a good real-world example (I'm updating my answer to make this clear).
Bozho
+4  A: 

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.)

Jon Skeet
thx .. anyway the logs are not written in separate file they are only written in the same file .So I think its Singleton ..whats your opinion?
JavaUser
Singletons are not about files, they are about objects. A singleton object can access a hundred files or it might not even require access to any file at all
dbemerlin
+1  A: 

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.

Thilo
+1  A: 

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.

Michael Aaron Safyan
A: 

In Java J2SE API - Two classes Calendar and Runtime are good examples of real time implementation of Singleton Pattern.

-Arun

akjain
Can you explain me the reason?
JavaUser
Calendar is not a singleton
Bozho
Restricting access to the constructor of a class automatically doesn't makes it a Singleton. The Calendar class constructor is protected, and the class offers a getInstance() method to get an instance of the class. However, each call to getInstance() gets a new instance of the class. So that isn't a *Singleton*
Narayan
Sorry my mistake, I should say that these classes can be implemented using Singleton. for eg: In application if you want all objects to use the same Calendar (Shared Calendar) then it can be implemented using Singleton pattern. Similarly Runtime is another example.
akjain