C# 工厂方法和保护级别问题

发布于 2024-11-16 18:25:01 字数 733 浏览 0 评论 0原文

我有一个基类和从该基类派生的几个子类。我在基类中还有一个静态函数,它接受一些参数,并根据输入参数(我的工厂方法)实例化并返回适当的子类。

现在这是我的问题:我只想允许从工厂方法实例化子类。但是如果我将子类的构造函数设置为受保护,则基类就看不到它们。我是否缺少一个访问修饰符,它允许基类调用子类构造函数,但不允许任何其他类调用它们?

内部看起来也不起作用...我想将对子类构造函数的访问限制为仅基类,同一程序集中还有其他类应该能够访问基工厂方法,但不能直接访问实例化任何子类。

希望我缺少一些非常简单的东西......

示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯角 2024-11-23 18:25:01

您可以将子类声明为 Base 中的私有嵌套类

You can declare the child classes as private nested classes inside Base

仅一夜美梦 2024-11-23 18:25:01

您是否尝试过在基类中声明子类?

public class Base {
    protected class ChildA {}
    protected class ChildB {}
}

Have you tried declaring the child classes within the base class?

public class Base {
    protected class ChildA {}
    protected class ChildB {}
}
撩动你心 2024-11-23 18:25:01

如果通过类型访问任何派生对象是有效的场景(假设派生类型仅覆盖实现并且不添加新功能),那么将派生类型嵌套私有类(如之前的答案所建议)的建议解决方案是最佳解决方案。

如果情况并非如此,那么我认为您陷入了不合理复杂性的情况。同一程序集中的代码无法访问 ChildAChildB 构造函数的原因是什么?毕竟是您可以控制的代码,因此您始终可以选择通过代码审查来制定/强制执行通过工厂方法进行初始化。

我知道有充分的理由不允许外部程序集自由实例化对象,除非通过严格控制的机制。在这种情况下,只需将构造函数标记为 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 and ChildB 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文