Consider the following example:
You are building a quote application, which contains a class that is responsible
for managing all of the quotes in the system. It is important that all quotes interact
with one and only one instance of this class. How do you structure your
design so that only one instance of this class is accessible within the application?
A simple solution to this problem is to create a QuoteManager class with a private
constructor so that no other class can instantiate it. This class contains a static instance
of QuoteManager that is returned with a static method named GetInstance(). The
code looks something like this:
public class QuoteManager
{
//NOTE: For single threaded applications only
private static QuoteManager _Instance = null;
private QuoteManager() {}
public static QuoteManager GetInstance()
{
if (_Instance==null)
{
_Instance = new QuoteManager ();
}
return _Instance;
}
//... functions provided by QuoteManager
}
It is likely that you have solved problems like this in a similar manner, as many other
developers have. In fact, pattern writers on the lookout for recurring problems and
solutions have observed this kind of implementation frequently, distilled the common
solution, and documented the problem-solution pair as the Singleton pattern
[Gamma95].
ایده رو متوجه شدید دوست عزیز؟
موفق باشید.