重写嵌套类函数或使用委托?**
我有一个基类,里面有一个嵌套类型。外部(基)类型中有一个函数,稍后会被它的子类型覆盖。事实上,从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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的建议是为内部类型函数创建一个委托,该委托由基类的构造函数启动:
如您所见,派生类型可以使用 < 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:
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 useAction
but any delegate you want.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.
在这种情况下:
您无法从类
A
派生并重写类B
中的func()
。从您的描述来看,A 派生类应该能够覆盖内部类
B
中的某些函数(或功能),这表明您可能应该重新考虑您的设计。要么提取B
并且不将其设为内部类,要么将您想要的功能通过这样的接口覆盖显式依赖项:无论如何,如果您想坚持自己的设计,那么我不会推荐使用委托方法,而不是选择覆盖,因为使用委托,如果您在子类中需要添加装饰,那么添加装饰会变得更加困难。
In this scenario:
You cannot derive from class
A
and overridefunc()
in classB
.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 extractB
and don't make it an inner class or make the functionality you want to override an explicit dependency via an interface like this: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.
这就是外部类如何充当内部服务类的策略。
请注意,不建议使用诸如
TemplateMethod
和Strategy
之类的模式名称作为真实的类名称,请使用在域中有意义的任何名称。同样适用于Outer
和Inner
。This is how the outer class can serve as a strategy to the inner service class.
Note that using pattern names such as
TemplateMethod
andStrategy
as real class names is not recommended, use whatever is meaningful in the domain. Same applies toOuter
andInner
.