当我只希望某些派生类能够访问基类中的方法时,应使用什么设计模式?
我这里有一个独特的问题/情况。尝试使其尽可能简单。我有一个基类(比如 Parent)和一大堆直接从基类(Parent)派生的派生类(比如 Child1、Child2 ..ChildN)。我想更改基类并添加一个“AVeryPrivilegedMethod”,该方法只能由 Child2 和 Child3 访问,而不能由任何其他 Children 访问(或者使其可配置,以便将来 Child5 也可以在将来使用它,只需进行最小的更改)。什么设计模式/架构模式适合这个要求?
使用的语言 - C#。
PS:我正在考虑使用 InternalVisibleTo 但是意识到这在汇编级别得到应用
I have a unique problem/situation here. Trying to make it as simple as possible. I have a base class (say Parent) and a whole bunch of derived classes (say Child1, Child2 ..ChildN) directly deriving from the base class (Parent). I want to change the base class and add a "AVeryPrivilegedMethod" which will only be accessible to Child2 and Child3 and not to any other Children (or make it configurable such that in future Child5 can also use it in future, with minimal changes). What design pattern /Architectural pattern will fit this bill?
Language used - C#.
PS: I was thinking about using InternalVisibleTo but realize that this gets applied at the assembly level
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
听起来好像您缺少另一个抽象类(
SpecialChild
,因为需要一个更好的名称),它继承自Parent
,但是>Child2
和Child3
是派生的。问自己这个问题:
Child2
和Child3
有什么不同,以至于它们本身具有共同的行为,但与所有其他子级有不同的行为?SpecialChild
对该行为进行建模,在您的问题中给出的示例中,将是实现AVeryPrivilegedMethod
的地方。It sounds as though you're missing another abstract class (
SpecialChild
for want of a better name) that inherits fromParent
but from whichChild2
andChild3
are derived.Ask yourself this question: what is different about
Child2
andChild3
such that they share common behaviour themselves, but have different behaviour to all of the other children?SpecialChild
models that behaviour and in the example you gave in your question would be the place to implementAVeryPrivilegedMethod
.我不明白这与“设计模式”有什么关系——这只是语言特性的问题。 C# 不具备允许这种轻松选择封装的语言功能。
我猜你的选择是在层次结构中插入一个新类,
BaseWithExtras
,派生自Base
,并让一些子类派生自Base
,并且其他来自BaseWithExtras
,或者不再担心它,只需使该方法可用于所有派生类。I don't see what this has to do with "design patterns" -- it's just a matter of language features. C# does not have a language feature that permits this sort of pick-and-choose encapsulation easily.
I guess your options are to either insert a new class in the hierarchy,
BaseWithExtras
, deriving fromBase
, and have some children derive fromBase
and others fromBaseWithExtras
, or to stop worrying about it and just make the method available to all derived classes.您可能想要进行另一个抽象级别:
然后每个子类都会继承适当的类:
You would want to make another level of abstraction:
Then each child class inherits the appropriate class:
如果您只能访问基类,我会说在基方法中对类的类型使用反射,并且只允许您想要正确使用基方法的类。如果情况并非如此,并且您有能力修改层次结构或派生类,只需从您的基派生另一个类来公开您感兴趣的方法,然后使您的类从中派生即可。
If you only have access to the base class, I'd say to use reflection on the type of the class in the base method, and only allow classes that you want to correctly use the base method. If that's not the case, and you have an ability to modify the hierarchy or the derived classes, just make another class derived from your base that exposes your method of interest, and make your classes derive from that.
可能没有好的选择,因为这不是标准的保护级别。这是一个选项
稍后,您可以这样称呼它:
这是假设您想要编译器检查(不在运行时使用反射来检查 Child2 和 Child3),并且出于某种原因需要您所说的层次结构。还有其他答案建议子类的新级别,这可能是您情况下的最佳答案。如果没有,这可能会有所帮助。
There are probably no good options, since this isn't a standard level of protection. Here's one option
Later, you call it like this:
This is assuming that you want compiler checking (not using reflection at runtime to check Child2 and Child3), and for some reason need the hierarchy you stated. There are other answers that suggest a new level of subclass, which may be the best answer in your situation. If not, this might help.
与依赖注入的良好旧关联怎么样(这样您可以在以后需要时更改它以允许其他类访问这些函数)。
然后,根据 Child,您可以注入
AllowPrivileges
或NoPrivileges
版本。How about good old association with Dependency Injection (so you can change it later if needed to allow other classes to access the functions).
Then depending on the Child, you can inject the
AllowPrivileges
orNoPrivileges
version.如果这些方法仅在某些子类中使用,那么将它们包含在继承层次结构中看起来不是一个好主意。这里我们想要实现的是实现重用,因此通过依赖注入进行组合将是一个好主意,但是如果您需要将该方法公开为类接口的一部分,那么 Mixin(如果在 C# 中可以的话)将是最好的选择去争取。
If those methods are going to be used in only certain child classes including them in the inheritance hierarchy doesnt look like a good idea . Here what we want to achieve is implementation reuse so composition through dependency injection would be a good idea, however if you need to expose that method as a part of your classes interface then Mixin(if it was possible in C#) would have been the thing to go for.