当我只希望某些派生类能够访问基类中的方法时,应使用什么设计模式?

发布于 2024-11-19 15:18:28 字数 449 浏览 2 评论 0原文

我这里有一个独特的问题/情况。尝试使其尽可能简单。我有一个基类(比如 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 技术交流群。

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

发布评论

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

评论(7

寂寞陪衬 2024-11-26 15:18:29

听起来好像您缺少另一个抽象类SpecialChild,因为需要一个更好的名称),它继承自Parent,但是>Child2Child3 是派生的。

                    Parent
                      | 
   |------------------|------------|----------|
Child1            SpecialChild   Child4    Child5
                      |
         |---------------------|
      Child2                 Child3

问自己这个问题:Child2Child3 有什么不同,以至于它们本身具有共同的行为,但与所有其他子级有不同的行为? SpecialChild 对该行为进行建模,在您的问题中给出的示例中,将是实现 AVeryPrivilegedMethod 的地方。

It sounds as though you're missing another abstract class (SpecialChild for want of a better name) that inherits from Parent but from which Child2 and Child3 are derived.

                    Parent
                      | 
   |------------------|------------|----------|
Child1            SpecialChild   Child4    Child5
                      |
         |---------------------|
      Child2                 Child3

Ask yourself this question: what is different about Child2 and Child3 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 implement AVeryPrivilegedMethod.

可爱咩 2024-11-26 15:18:29

我不明白这与“设计模式”有什么关系——这只是语言特性的问题。 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 from Base, and have some children derive from Base and others from BaseWithExtras, or to stop worrying about it and just make the method available to all derived classes.

感悟人生的甜 2024-11-26 15:18:29

您可能想要进行另一个抽象级别:

public class Parent { }
public class MethodContainer : Parent { public void SomeMethod() { } }

然后每个子类都会继承适当的类:

// Does not have method
public class ChildA : Parent

// Has Method
public class ChildB: MethodContainer

You would want to make another level of abstraction:

public class Parent { }
public class MethodContainer : Parent { public void SomeMethod() { } }

Then each child class inherits the appropriate class:

// Does not have method
public class ChildA : Parent

// Has Method
public class ChildB: MethodContainer
故人的歌 2024-11-26 15:18:29

如果您只能访问基类,我会说在基方法中对类的类型使用反射,并且只允许您想要正确使用基方法的类。如果情况并非如此,并且您有能力修改层次结构或派生类,只需从您的基派生另一个类来公开您感兴趣的方法,然后使您的类从中派生即可。

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.

丶视觉 2024-11-26 15:18:29

可能没有好的选择,因为这不是标准的保护级别。这是一个选项

 class Parent
 {
       private void AVeryPrivilegedMethod() {}
       public static void AVeryPrivilegedMethod(Child2 c) { ((Parent)c).AVeryPrivilegedMethod(); }
       public static void AVeryPrivilegedMethod(Child3 c) { ((Parent)c).AVeryPrivilegedMethod(); }
 }

稍后,您可以这样称呼它:

 Child2 c = new Child2();
 Parent.AVeryPrivilegedMethod(c);

这是假设您想要编译器检查(不在运行时使用反射来检查 Child2 和 Child3),并且出于某种原因需要您所说的层次结构。还有其他答案建议子类的新级别,这可能是您情况下的最佳答案。如果没有,这可能会有所帮助。

There are probably no good options, since this isn't a standard level of protection. Here's one option

 class Parent
 {
       private void AVeryPrivilegedMethod() {}
       public static void AVeryPrivilegedMethod(Child2 c) { ((Parent)c).AVeryPrivilegedMethod(); }
       public static void AVeryPrivilegedMethod(Child3 c) { ((Parent)c).AVeryPrivilegedMethod(); }
 }

Later, you call it like this:

 Child2 c = new Child2();
 Parent.AVeryPrivilegedMethod(c);

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.

dawn曙光 2024-11-26 15:18:29

与依赖注入的良好旧关联怎么样(这样您可以在以后需要时更改它以允许其他类访问这些函数)。

public class Parent {
   private PrivilegedFunctions p;
   public Parent(PrivilegedFunctions inP) { p = inP; }
}

public interface PrivilegedFunctions {
   void SomeFuncHere();
}

public class AllowPrivileges : PrivilegedFunctions {
   public void AllowPrivileges () { }

   public void SomeFuncHere()
   { 
      // Actual implementation
   }
}

public class NoPrivileges : PrivilegedFunctions {
   public void NoPrivileges () { }

   public void SomeFuncHere()
   { 
      // No implementation
   }
}

public class Child1 : Parent {
   public Child1(PrivilegedFunctions inP) : base(inP) { }
}

然后,根据 Child,您可以注入 AllowPrivilegesNoPrivileges 版本。

// Child with privileges
Child1 with_priv = new Child1(new AllowPrivileges());
with_priv.SomeFuncHere(); // Does privileged operation
// Child without privileges
Child1 without_priv = new Child1(new NoPrivileges());
without_priv.SomeFuncHere(); // Does nothing

How about good old association with Dependency Injection (so you can change it later if needed to allow other classes to access the functions).

public class Parent {
   private PrivilegedFunctions p;
   public Parent(PrivilegedFunctions inP) { p = inP; }
}

public interface PrivilegedFunctions {
   void SomeFuncHere();
}

public class AllowPrivileges : PrivilegedFunctions {
   public void AllowPrivileges () { }

   public void SomeFuncHere()
   { 
      // Actual implementation
   }
}

public class NoPrivileges : PrivilegedFunctions {
   public void NoPrivileges () { }

   public void SomeFuncHere()
   { 
      // No implementation
   }
}

public class Child1 : Parent {
   public Child1(PrivilegedFunctions inP) : base(inP) { }
}

Then depending on the Child, you can inject the AllowPrivileges or NoPrivileges version.

// Child with privileges
Child1 with_priv = new Child1(new AllowPrivileges());
with_priv.SomeFuncHere(); // Does privileged operation
// Child without privileges
Child1 without_priv = new Child1(new NoPrivileges());
without_priv.SomeFuncHere(); // Does nothing
疑心病 2024-11-26 15:18:29

如果这些方法仅在某些子类中使用,那么将它们包含在继承层次结构中看起来不是一个好主意。这里我们想要实现的是实现重用,因此通过依赖注入进行组合将是一个好主意,但是如果您需要将该方法公开为类接口的一部分,那么 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.

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