I know what a singleton pattern is and i also know that logging is an example of Singleton. Can somebody help me identifying scenario(s) where singleton can be proved as a cherry on a cake. I am looking for some real time, project specific scenario examples where we can use singleton design pattern. Assume that i am working on a banking (Le'ts say RBS) or telecom project (bharti airtel).
The scenario that Mishra describes is better solved by injecting a repository of countries into whatever method or constructor needs it. It should implement an interface, so that you can write another implementation wraps around an actual repository, and performs caching. It's even better to let your application container framework handle caching though.
In my opinion, in a major project following strategy design is better whether it is implemented by you or done emphasized by a framework you use
Stephan van Hulst wrote:I don't understand your description of the scenario in your last post. Can you elaborate, or explain with a small example?
yeah sure, I will try my best to elaborate if I am not able to do it correctly do inform me.
Let us assume that we are developing an API that has some arbitrary target and function. Let's call this API "McRitchie"
We don't need to look at McRitchie as a whole, rather than just a small part. One particular functionality that McRitchie is trying to implement is caching databases. In the cache, the basic implementation can be something like a nested Hashmap that holds the records of the table.
One assumption that we are making is that the table we want to cache is static or shall I put it like "Only static table can be cached by McRitchie"
If it were an application being developed we could have made an object of the cache ourselves and passed it around to whatever part of the application needing it. But since we are making an API we have to consider that rather than us populating the cache we will have our user (some other programmer) do that by using our API. Now, we have already assumed that the tables going to be cached are static (they won't be updated or modified). Since the tables are static, it would be really unnecessary to be able to create multiple objects of a cache that won't be changing in the runtime once loaded. But how can we guarantee such behaviour, can we guarantee that our user in all cases will only create at most one object (We can't, refer to einstein's quote about human stupidity and universe to get an idea ), to achieve the one object per runtime of a static cache we can implement the cache class as singleton.
If I was able to explain well give me a thumbs up, otherwise please be a positive critique and provide me with some of your expert insight.
Oh wait a minute, you're talking about a table that can't be changed. "Static" isn't a good word there since it makes me think you're talking about static variables, which means you would be confusing objects and variables. Sorry about that.
But I don't see why the immutability of a table is important for this. On the contrary, if the table were mutable it would be much more important to make sure there was only one instance of the table being used.
Singleton pattern is a design solution where an application wants to have one and only one instance of any class, in all possible scenarios without any exceptional condition. It has been debated long enough in java community regarding possible approaches to make any class singleton.
I tried to answer the OP's question but maybe I deviated. I will ofcourse learn to answer better by keeping at it.
This community is so good and helping. You guys are great always ready to help and offer positive criticism which ofcourse is one of the key things missing from my environment here.
Gee shucks! You gert us all 'mbarrassed!
Mishra Saurabh wrote:. . . This community is so good and helping. . . . .
That's a pleasure
Application settings are NOT a good example.
Stephan van Hulst wrote:What are you going to do if you want to change the application properties programmatically, while running? Surely you're not going to modify the global instance, are you? And how are you going to unit test classes whose behavior depends on application settings?
Application settings are NOT a good example.
Using a program we would not intend to change value of Application Properties file
This option just makes Interviewer mouth keep shut on further questions