Registry of Singletons

Registry of Singletons

By  Guy Baseke | 6 Jan 2004
Desgin Patterns - Object creation - Singleton.
  1.20 (4 votes)
Sponsored Links

Introduction

Singletons ensure that a class only has one instance, and provide a global point of access to it.

Motivation

It is important for some classes to have exactly one instance. There should be only one dock manager for an application, there should be only one work bench for an application.

How do we ensure that a class has only one instance? A global variable makes an object accessible, but it doesn't keep you from creating multiple objects. For people who come from a C/C++ programming background, it is disturbing to find that C# does not support global variables.

Implementation

There are many ways to solve this problem and I will not go into them, but will explain a simple way of solving the problem by using a registry of singletons.

Most of the time, the solution will be a class that is written so that only one instance can be created. A common way to do this is to hide the operation that creates the instance behind a class operation that guarantees that only one instance is created.

Why a registry of singletons? Because an application will most of the time require more than just one type object which should be unique, therefore the need of a registry of singletons or a list of singletons used in the application

The registry maps between string names and singletons. When an instance needs a singleton, it consults theregistry, asking for the singleton by name.

Code

using System; 
using System.Collections;
namespace MySingleton
{
    /// <summary> 
    /// Summary description for CSingleton. 
    /// </summary>
    public class CSingleton 
    {
        protected CSingleton(){} 
        static SortedList _register = new SortedList();
        public static void Register(string name, ISingleton value) 
        {
            // Cannot overwrite a key 
            if ( _register.ContainsKey(name)) 
                throw new Exception("Key '" + name + "' already registered"); 
            else 
                _register[name] = value; 
        }
        public static ISingleton Lookup(string name) 
        {
            return (ISingleton)_register[name]; 
        }
    }
    public interface ISingleton
    {
        string Title
        {
            get;
            set;
        }
    }
} //~Namespace

The idea is:

  1. To create an interface (ISingleton) that will be implemented by every class that needs to keep a singleton. E.g.:
    class A: ISingleton
    {
        public A() {}
    }
    class B: ISingleton
    {
        private string _greeting;
        public B(string str){ _greeting = str;}
        public string Greeting
        {
            get{ return _greeting;}
            set{ _greeting = value;}
        }
    }
  2. To register the object with the singleton:
    CSingleton.Register("ClassA", (ISingleton) new A());
    CSingleton.Register("ClassB", (ISingleton) new B("Hello"));
  3. To used the singleton object anywere in your code:
    A classAObj = (A) CSingleton.Lookup("ClassA");
    B classBObj = (B) CSingleton.Lookup("ClassB");
    classBObj.Greeting;

Conclusion

Hope this article will benefit you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值