Tuesday, April 17, 2007

Singleton

Sometime, only one instance of a certain class is needed. In this case, Singleton pattern helps.

In the figure above, the user is required to get the unique instance by calling Instance() which creates and maintains the object. The constructor is usually hidden by setting it protected so that the user can't instantiate it manually.

Of course, the idea can be applied in the case a fixed/variable number of instances are allowed to exists and the user shouldn't get involved in the maintainance of them. Static operations can be used, however it is less flexible because in this case there can be only one instance.

No comments: