如何做单继承(How to do inheritance with singleton)

2019-09-24 06:47发布

我想知道如何从一个类的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?

Answer 1:

我总是发现它更容易实现这种类型的使用通用的解决方案:

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>
{
    ...
}


Answer 2:

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

我只是用一个独立的类来存储共享成员和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);
    }
}


Answer 3:

你可以在你的基类变更单的意思是这样的:

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


Answer 4:

由于_instance是这两类民营这是两个不同的变量。 _instance必须得到保护中共享秩序。 派生类不能访问基类的私有成员!



文章来源: How to do inheritance with singleton