Why is a singleton pattern useful? In what circums...
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Xuemin_Guan
Posted On:   Wednesday, March 7, 2001 06:51 PM

Why is a singleton pattern useful? In what circumstance?

Re: Why is a singleton pattern useful? In what circums...

Posted By:   Thiru_Kumar  
Posted On:   Wednesday, August 8, 2001 06:09 PM

Re: Why is a singleton pattern useful? In what circums...

Posted By:   Tudor_Girba  
Posted On:   Friday, May 4, 2001 09:23 AM

The Singleton Pattern applies whenever you want to be sure that only one instance of a class will exist in your code. But let's say that if you own the code you write, you might now that, for example, there can only be one instance of a graphic manager. So you'll have a global instance. But, if you don't own the code (for example you work in a team) and you still want to exist only one instance of that graphic manager. What will you do? How can you enforce that "only" one instance? It's simple you apply the Singleton Pattern. You will gain two things:
1. you won't have to tell the other guys how to use your class
2. you will now for sure that there will be only one instance of that class.

Re: Why is a singleton pattern useful? In what circums...

Posted By:   Michael_Wax  
Posted On:   Thursday, April 5, 2001 02:37 PM

Use the singleton pattern when you want one object to be shared by multiple threads. The sharing could be important because the object represents a scarce resource, or because the object is expensive to create.

For example, you would want all threads in an application to share a pool of database connections, because a database can have a limited number of open connections (and the connections will be used most efficiently if they are shared). In another example, if you created a tree structure by parsing a very large file, in a process which took minutes to complete, you probably would want to store this structure as a singleton so that it would not be created over and over.

About | Sitemap | Contact