具有多个子类的抽象单例继承
本文关键字:抽象 单例 继承 子类 | 更新日期: 2023-09-27 18:14:08
继续这里给出的答案,是否有一种方法允许MyChildSingleton是抽象的,然后让它的子类定义自己的构造函数?我想做的一个例子:
public abstract class SingletonBase<T>
where T : SingletonBase<T>, new()
{
private static T _instance = new Lazy<T>(() => new T());
public static T Instance
{
get
{
return _instance;
}
}
}
public abstract class DefaultConfigData: SingletonBase<DefaultConfigData>
{
// This class won't compile since it's abstract, and SingletonBase<T>
// has a new() restriction on T
// Also, this class is immutable and doesn't change state
public virtual string SomeData { get; } = "My Default Data String";
public virtual double MoreData { get; } = 2.71;
public virtual double SomeFunction(double num)
{ return num + 2*MoreData; }
public DefaultConfigData() { ; /* nothing to do here */ }
// Another 50 or so default values/functions...
// enough to be tedious to redefine in multiple places,
// and adding a constructor that takes every value would be ridiculous.
// It would be possible to encapsulate this data, but I'm not
// yet sure how this should be done, so I haven't gone there yet
}
public class SpecificConfigData1: DefaultConfigData
{
public override string SomeData { get; } = "A Different String";
public SpecificConfigData1() { ; /* nothing to do here */ }
}
public class SpecificConfigData2: DefaultConfigData
{
public override double MoreData { get; } = 3.14;
public SpecificConfigData2() { ; /* nothing to do here */ }
}
// Downstream developers may need to define additional ConfigData classes
public class Library
{
public static double doSomething(DefaultConfigData data) { return data.MoreData + 2.0; }
}
public class Program
{
private readonly DefaultConfigData data;
public Program(bool choice)
{
data = (choice) ? SpecificConfigData1.Instance : SpecificConfigData2.Instance;
}
public static void Main()
{
Program prog = new Program(/*run-time user input here*/);
Console.PrintLine(Library.doSomething(prog.data));
}
}
使用单例模式似乎是一个好主意,因为对于每个特定的子类,数据只需要存在于一个地方,并且由于它是不可变的,这避免了与单例相关的大多数问题(全局可变状态等)。在抽象基类中提供单例功能将避免将私有实例和公共get属性放在样板中,这就是我现在在每个子类中所做的。这真的不是一个太繁重的要求,我相信我可以接受它。
我不想让DefaultConfigData和它的数据静态,因为这样我就不能从它继承,让我的库函数知道如何与它交互(不支持c#中的元类)。另外,我不想使用接口,因为太多的功能是共享的,我不能在接口中定义。
如果我正在尝试的方法无法完成,或者其他方法更容易,我也欢迎对其他方法的评论。我知道工厂模式也可以在这里工作,这是我打算最终尝试的东西。
最后,为什么这是一个问题?为什么不决定让抽象类满足new()要求,只要它们的任何子类也满足new()要求?
注意我的"用户"是其他内部开发人员/我未来的自己。源代码通常是可交付的,在这种环境下,将检查推到运行时是可以的。
我能想到的最简单的解决方案是使用工厂模式。另一个解决方案是您需要保持DefaultConfigData
类的通用性,如:
public abstract class DefaultConfigData<T>: SingletonBase<T>
where T : DefaultConfigData<T>, new()
{ }
问题是当你想在任何地方使用DefaultConfigData
时,你必须使该方法或类泛型,如doSomething
方法:
public static double doSomething<T>(DefaultConfigData<T> data)
where T : DefaultConfigData<T>, new()
{
return data.MoreData + 2.0;
}
你可以猜到,这会让真的很烦人。那么,回到工厂模式:
public static class MyFactory<T>
{
private static Lazy<T> _instance = new Lazy<T>(CreateUsingReflection);
public static T Instance
{
get
{
return _instance.Value;
}
}
private static T CreateUsingReflection()
{
BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
ConstructorInfo ctor = typeof(T).GetConstructor(flags, null, Type.EmptyTypes, null);
return (T)ctor.Invoke(null);
}
}
你可以这样使用:
SpecificConfigData1 scd1 = MyFactory<SpecificConfigData1>.Instance;
注意,您的任何类都不需要从MyFactory
继承。只要有一个无参数构造函数,就可以自由地创建从任何东西(或什么都没有)继承的类。您可以使用where T : new()
限制MyFactory<T>
中的T
,在这种情况下,您将获得编译时保证该类支持无参数公共构造函数,并且可以通过仅使用new T()
创建类来避免反射。如果没有new()
限制,它可以使用私有构造函数创建单例,但是您将失去在编译时检查是否存在无参数构造函数的功能。
实际上,还有另一个解决方案。它要复杂得多,但如果你充分利用它,它就会非常强大:一个IoC容器,如Autofac、Unity等。这些可以帮助管理实例,这样您就可以指定应该是单例的类型。我不会详细说明如何使用,因为这是一个完全不同的主题,并且需要多个段落来解释基本内容。