C# 工厂方法和保护级别问题
我有一个基类和从该基类派生的几个子类。我在基类中还有一个静态函数,它接受一些参数,并根据输入参数(我的工厂方法)实例化并返回适当的子类。
现在这是我的问题:我只想允许从工厂方法实例化子类。但是如果我将子类的构造函数设置为受保护,则基类就看不到它们。我是否缺少一个访问修饰符,它允许基类调用子类构造函数,但不允许任何其他类调用它们?
内部看起来也不起作用...我想将对子类构造函数的访问限制为仅基类,同一程序集中还有其他类应该能够访问基工厂方法,但不能直接访问实例化任何子类。
希望我缺少一些非常简单的东西......
示例:
public class Base
{
public Base CreateChild(string childType)
{
if(childType == "A")
return new ChildA();
if(childType == "B")
return new ChildB();
return null;
}
}
public class ChildA
{
protected ChildA() // This doesn't work, since now base class can't call this!
{
}
}
public class ChildB
{
protected ChildB()
{
}
}
I have a base class and several subclasses derived from that base class. I also have a static function in the base class that takes some parameters, and instantiates and returns an appropriate subclass based on input parameters ( my factory method.)
Now here's my problem: I want to ONLY allow instantiation of the subclasses FROM the factory method. But if I set the constructors of the subclasses to be protected, the base class can't see them. Is there an access modifier I'm missing that would allow the base class to call the subclasses constructors, but not not allow any other classes to call them?
Internal doesn't look like it will work either...I want to limit access to the subclass constructors to just the base class, there are other classes in the same assembly that should be able to access the base factory method and but not directly instantiate any of the subclasses.
Hopefully there's something really simple I'm missing...
Example:
public class Base
{
public Base CreateChild(string childType)
{
if(childType == "A")
return new ChildA();
if(childType == "B")
return new ChildB();
return null;
}
}
public class ChildA
{
protected ChildA() // This doesn't work, since now base class can't call this!
{
}
}
public class ChildB
{
protected ChildB()
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将子类声明为 Base 中的私有嵌套类
You can declare the child classes as private nested classes inside Base
您是否尝试过在基类中声明子类?
Have you tried declaring the child classes within the base class?
如果通过基类型访问任何派生对象是有效的场景(假设派生类型仅覆盖基实现并且不添加新功能),那么将派生类型嵌套
私有
类(如之前的答案所建议)的建议解决方案是最佳解决方案。如果情况并非如此,那么我认为您陷入了不合理复杂性的情况。同一程序集中的代码无法访问
ChildA
和ChildB
构造函数的原因是什么?毕竟是您可以控制的代码,因此您始终可以选择通过代码审查来制定/强制执行通过工厂方法进行初始化。我知道有充分的理由不允许外部程序集自由实例化对象,除非通过严格控制的机制。在这种情况下,只需将构造函数标记为
internal
即可。否则,我不确定您是否可以实现您假装的目标,而无需仅为此基类及其派生类创建特定的程序集。绝对没有访问修饰符可以使派生类中的静态方法只能在其基类中可见。
If accessing any derived object through the base type is a valid scenario (let's say derived types only override base implementations and do not add new functionality) then the proposed solution of making the derived types nested
private
classes (as previous answers propose) is the best solution.If that's not the case then I think you are falling into a case of unjustified complexity. What is the reason why code from your same assembly can not access
ChildA
andChildB
constructors? It is after all code you can control, so you can always choose to make / enforce via code review that he initalization is through the factory method.I understand there is valid reasons to not let external assemblies freely instantiate objects except through a tightly controlled mechanism. In this case just marking the constructors as
internal
would do.Otherwise, I'm not sure you can achieve what you are pretending without creating a specific assembly just for this base class and its derived classes. There is definitely no access modifier that would make a static method in a derived class only visible from it's base class.