Чтобы сократить вопрос, это немного упрощено.Суть в том, чтобы заставить исключительный вызов всех базовых конструкторов из производных и запретить все остальные конструкторы.
Форсирование одного исключительного конструктора с помощью абстрактных шаблонных работ:
public abstract class AbstractTemplatePattern
{
protected AbstractTemplatePattern(Foo foo)
{
// do the same work for every derived class
}
}
class DerivedTemplatePattern : AbstractTemplatePattern
{
// exclusive constructor forced by template (no others allowed)
public DerivedTemplatePattern(Foo foo) : base(foo) { }
// possible too, force calling the base contructor is the main thing here!
public DerivedTemplatePattern() : base(new Foo()) { }
public DerivedTemplatePattern(Bar bar) : base(new Foo()) { }
// not allowed constructor examples
public DerivedTemplatePattern() { }
public DerivedTemplatePattern(Bar bar) { }
}
Очевидное расширение n исключительных конструкторовне заставит иметь n исключительных конструкторов в производном классе, это вызовет только один из них:
public abstract class AbstractTemplatePattern
{
protected AbstractTemplatePattern(Foo foo)
{
// do the same work for every derived class
}
protected AbstractTemplatePattern(Bar bar)
{
// do the same work for every derived class
}
}
class DerivedTemplatePattern : AbstractTemplatePattern
{
// no error, but this should not be allowed, as the template gives two constructors
public DerivedTemplatePattern(Foo foo) : base(foo) { }
}
Можно ли добиться этого с помощью абстрактного шаблона?
Если это помогает: реализацияфабричный образец был бы выбором.