重写嵌套类函数或使用委托?**

发布于 2024-11-03 01:17:44 字数 558 浏览 0 评论 0原文

我有一个基类,里面有一个嵌套类型。外部(基)类型中有一个函数,稍后会被它的子类型覆盖。事实上,从OO的角度来看,这个函数属于内部类型,但我仍然需要它,被子类型覆盖基类

我应该使用此函数作为来自内部类型回调,还是将其移至内部类型内部,然后使用子类型 code> 从那里覆盖它?

编辑:添加示例代码

class A
{
    protected void func() { /* do something */ }
    class B { /**/ }
}

// OR

class A
{
    class B
    {
        protected void func() { /* do something */ }
    }
}

// Then

class C : A
{
    override func() { /**/ }
}

I have a base class which has a nested type, inside. There's a function in the outer (base) type which would be overridden by it's children later. In fact this function belongs to the inner type from the OO prespective but still I need it, to be overridden by subtypes of the base class.

Should I use this function as a callback from the inner type or just move it inside the inner type and let's the subtypes to override it from there?

EDIT: Sample code added

class A
{
    protected void func() { /* do something */ }
    class B { /**/ }
}

// OR

class A
{
    class B
    {
        protected void func() { /* do something */ }
    }
}

// Then

class C : A
{
    override func() { /**/ }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

你列表最软的妹 2024-11-10 01:17:44

我的建议是为内部类型函数创建一个委托,该委托由基类的构造函数启动:

internal class BaseClass
{
    public BaseClass(Action myAction)
    {
        this.innerType = new InnerType(myAction);
    }

    public BaseClass()
    {
        // When no function delegate is supplied, InnerType should default to
        // using its own implementation of the specific function
        this.innerType = new InnerType();
    }
}

如您所见,派生类型可以使用 < code>:base (overridenAction) 他们可以在最里面的类型中提供自己的函数实现。当然,您没有义务使用 Action,而是使用您想要的任何委托。

My suggestion is to crate a delegate for the inner type function which is initiated by the constructor of the base class:

internal class BaseClass
{
    public BaseClass(Action myAction)
    {
        this.innerType = new InnerType(myAction);
    }

    public BaseClass()
    {
        // When no function delegate is supplied, InnerType should default to
        // using its own implementation of the specific function
        this.innerType = new InnerType();
    }
}

As you see, deriving types can call the base constructor with :base (overridenAction) where they can provide their own implementation of the function right to the innermost type. Of course, you are not obligated to use Action but any delegate you want.

2024-11-10 01:17:44

IMO你所描述的看起来像策略设计模式。考虑使用这种模式。您的代码将更易于维护,因为它包含易于识别的模式。你也可以看看状态设计模式,通常你必须在这两者之间进行选择,它们是紧密相连的连接。

IMO what you are describing looks like The Strategy design pattern. Consider using this pattern. Your code would be much more maintainable as it contains well recognizable pattern. You also can take a look at state design pattern, usually you have to choose between these two, they are closely connected.

旧梦荧光笔 2024-11-10 01:17:44

在这种情况下:

class A
{
    class B
    {
        protected void func() { // do something }
    }
}

您无法从类 A 派生并重写类 B 中的 func()

从您的描述来看,A 派生类应该能够覆盖内部类 B 中的某些函数(或功能),这表明您可能应该重新考虑您的设计。要么提取 B 并且不将其设为内部类,要么将您想要的功能通过这样的接口覆盖显式依赖项:

class A
{
    private B _MyB;
    public A(ISomeBehaviour behaviour)
    {
        _MyB = new B(behaviour);
    }
}

无论如何,如果您想坚持自己的设计,那么我不会推荐使用委托方法,而不是选择覆盖,因为使用委托,如果您在子类中需要添加装饰,那么添加装饰会变得更加困难。

In this scenario:

class A
{
    class B
    {
        protected void func() { // do something }
    }
}

You cannot derive from class A and override func() in class B.

From your description it seems that A-derived classes should be able to override some function (or functionality) in the inner class B which indicates that you maybe should rethink your design. Either extract B and don't make it an inner class or make the functionality you want to override an explicit dependency via an interface like this:

class A
{
    private B _MyB;
    public A(ISomeBehaviour behaviour)
    {
        _MyB = new B(behaviour);
    }
}

In anyway if you want to stick with your design then I would not recommend the delegate approach and rather choose the override because with the delegates it makes it harder to add decoration if that is all you need in your child classes.

夏尔 2024-11-10 01:17:44

这就是外部类如何充当内部服务类的策略。

请注意,不建议使用诸如 TemplateMethodStrategy 之类的模式名称作为真实的类名称,请使用在域中有意义的任何名称。同样适用于OuterInner

public class Consumer
{
    public void Foo()
    {
        IOuterFoo fooService = new Derived();
        fooService.OuterFoo();
    }
}

// ...

public interface IOuterFoo
{
    void OuterFoo();
}

abstract class Base : Base.IStrategy, IOuterFoo
{
    public void OuterFoo() { _service.Foo(); }

    private readonly InnerService _service;

    protected Base() { _service = new InnerService(this); }

    private interface IStrategy { void Foo(); }

    private class InnerService
    {
        private readonly IStrategy _strategy;
        public InnerService(IStrategy strategy) { _strategy = strategy; }
        public void Foo() { _strategy.Foo(); }
    }

    void IStrategy.Foo() { TemplateMethodFoo(); }

    protected abstract void TemplateMethodFoo();
}

class Derived : Base
{
    protected override void TemplateMethodFoo()
    {
        throw new NotImplementedException();
    }
}

This is how the outer class can serve as a strategy to the inner service class.

Note that using pattern names such as TemplateMethod and Strategy as real class names is not recommended, use whatever is meaningful in the domain. Same applies to Outer and Inner.

public class Consumer
{
    public void Foo()
    {
        IOuterFoo fooService = new Derived();
        fooService.OuterFoo();
    }
}

// ...

public interface IOuterFoo
{
    void OuterFoo();
}

abstract class Base : Base.IStrategy, IOuterFoo
{
    public void OuterFoo() { _service.Foo(); }

    private readonly InnerService _service;

    protected Base() { _service = new InnerService(this); }

    private interface IStrategy { void Foo(); }

    private class InnerService
    {
        private readonly IStrategy _strategy;
        public InnerService(IStrategy strategy) { _strategy = strategy; }
        public void Foo() { _strategy.Foo(); }
    }

    void IStrategy.Foo() { TemplateMethodFoo(); }

    protected abstract void TemplateMethodFoo();
}

class Derived : Base
{
    protected override void TemplateMethodFoo()
    {
        throw new NotImplementedException();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文