2

我想知道如何从具有单例的类“BaseClient”继承,并能够在继承的类中使用来自基类单例的基本成员的相同实例。

public class BaseClient
{
    protected string _url;
    protected string _username;
    protected string _password;

    private static BaseClient _instance;
    private static readonly object padlock = new object();

    public static BaseClient Instance
    {
        get
        {
            lock (padlock)
            {
                if (_instance == null)
                {
                    _instance = new BaseClient(true);
                }
                return _instance;
            }
        }
    }

    public void SetInfo(string url, string username, string password)
    {
        _url = url;
        _username = username;
        _password = password;
    }

    public string GetVersion()
    {
        //MyService is a simple static service provider
        return MyService.GetVersion(_url, _username, _password);
    }
}

public class Advanced : BaseClient
{
    private static AdvancedClient _instance;
    private static readonly object padlock = new object();

    public static AdvancedClient Instance
    {
        get
        {
            lock (padlock)
            {
                if (_instance == null)
                {
                    _instance = new AdvancedClient(true);
                }
                return _instance;
            }
        }
    }

    public void DoAdvancedMethod()
    {
        MyService.DoSomething(_url, _username, _password);
    }
}

因此,如果我使用 BaseClient.Instance.SetInfo("http://myUrl", "myUser", "myPassword"); 然后 AdvancedClient.Instance.DoAdvancedMethod(),AdvancedClient 单例将使用与 BaseClient 单例相同的基本成员实例?

4

4 回答 4

3

我一直发现使用泛型实现这种类型的解决方案要容易得多:

public class Singleton<T> where T : class
{

    protected Singleton()
    {
    }

    public static T Instance
    {
        get { return SingletonFactory.instance; }
    }

    public void SetInfo(string url, string username, string password)
    {
        ...
    }

    public string GetVersion()
    {
        ...
    }

    class SingletonFactory
    {

        internal static readonly T instance;

        static SingletonFactory()
        {
            ConstructorInfo constructor = typeof(T).GetConstructor(
                       BindingFlags.Instance | BindingFlags.NonPublic,
                       null, new System.Type[0],
                       new ParameterModifier[0]);

            if (constructor == null)
                throw new Exception(
                    "Target type is missing private or protected no-args constructor: type=" + 
                    typeof(T).FullName);
            try
            {
                instance = constructor.Invoke(new object[0]) as T;
            }
            catch (Exception e)
            {
                throw new Exception(
                    "Failed to create target: type=" + typeof(T).FullName, e);
            }
        }

    }

}

public class Advanced : Singleton<Advanced>
{
    ...
}
于 2012-10-19T03:54:54.557 回答
1

开个玩笑:)这是我的解决方案:

我只是使用一个独立的类来存储共享成员,并在创建 AdvancedClient 单例时检索 BaseClient 。

public class ClientInfo
{
    public string Url { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

public class BaseClient
{
    protected ClientInfo _info;

    private static BaseClient _instance;
    private static readonly object padlock = new object();

    public static BaseClient Instance
    {
        get
        {
            lock (padlock)
            {
                if (_instance == null)
                {
                    _instance = new BaseClient(true);
                }
                return _instance;
            }
        }
    }

    public ClientInfo Info
    {
        get
        {
            if(_info == null)
            {
                _info = new ClientInfo();
            }

            return _info;
        }
    }

    public void SetInfo(string url, string username, string password)
    {
        Info.Url = url;
        Info.Username = username;
        Info.Password = password;
    }

    public string GetVersion()
    {
        //MyService is a simple static service provider
        return MyService.GetVersion(Info.Url, Info.Username, Info.Password);
    }
}

public class Advanced : BaseClient
{
    private static AdvancedClient _instance;
    private static readonly object padlock = new object();

    public static AdvancedClient Instance
    {
        get
        {
            lock (padlock)
            {
                if (_instance == null)
                {
                    _instance = new AdvancedClient(true);
                    _instance._info = BaseClient.Instance.Info;
                }
                return _instance;
            }
        }
    }

    public void DoAdvancedMethod()
    {
        MyService.DoSomething(Info.Url, Info.Username, Info.Password);
    }
}
于 2012-10-22T13:45:44.847 回答
0

您可以将基类中单例的含义更改为如下所示:

public static BaseClient Instance
{
    get
    {
        lock (padlock)
        {
            if (_instance == null)
            {
                _instance = (BaseClient)(new AdvancedClient(true));
            }
            return _instance;
        }
    }
}
于 2012-10-18T19:56:24.733 回答
0

由于_instance在两个类中都是私有的,因此这是两个不同的变量。_instance必须受到保护才能共享。派生类不能访问基类的私有成员!

于 2012-10-18T20:06:59.857 回答