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 .
Please give me a real time example for singleton pattern . Different threads accessing
Share
Yes, but only if all threads access the same file, and you are using a custom implementation (not
java.io.File, perhaps a wrapper)Singletons (being often a bad choice) are classes that can have only one instance throughout the whole program.
For example a
SingletonConfigFilemight look like this. Have in mind that:.
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”.