开发者

What design pattern to use when I want only some derived classes to have access to a method in base class?

开发者 https://www.devze.com 2023-03-19 05:39 出处:网络
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 f

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


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.


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.


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


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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消