Reputation: 31
I want to know how to inherit from a class 'BaseClient' that has a singleton and be able to use same instance of basic members from base class singleton in the inherited class too.
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);
}
}
So if I use BaseClient.Instance.SetInfo("http://myUrl", "myUser", "myPassword"); and then AdvancedClient.Instance.DoAdvancedMethod(), the AdvancedClient singleton will use the same base member instance as the BaseClient singleton?
Upvotes: 2
Views: 7019
Reputation: 31
Just kidding :) here's my solution:
I simply use an independent class to store the shared members and on AdvancedClient singleton creation, retrieve the BaseClient one.
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);
}
}
Upvotes: 1
Reputation: 1139
I've always found it much easier to implement this type of solution using a generic:
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>
{
...
}
Upvotes: 3
Reputation: 112342
Since _instance
is private in both classes these are two different variables. _instance
would have to be protected in order to be shared. The derived class cannot access a private member of the base class!
Upvotes: 0
Reputation: 31077
You could change the meaning of the singleton in your base class to something like this:
public static BaseClient Instance
{
get
{
lock (padlock)
{
if (_instance == null)
{
_instance = (BaseClient)(new AdvancedClient(true));
}
return _instance;
}
}
}
Upvotes: 0