1

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

ram
  • 11,033
  • 16
  • 59
  • 86

7 Answers7

4

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.

mqp
  • 64,209
  • 13
  • 90
  • 122
  • I think I've had similar questions in the past, where I'd think of limiting other classes to a privileged function as a "security" measure, which really don't make sense. As mquander said, it's better to just make two classes or just let anyone subclass use it. You can either decide whether to access it in the subclass or the base class, but either way, somewhere in your code you have to specify which classes are going to use that function. Are you afraid of calling it accidentally or something the child classes? Trying to keep users from writing plugins to that class? – voodoogiant Jul 11 '11 at 17:30
3

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
Tejs
  • 38,896
  • 8
  • 64
  • 81
3

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.

razlebe
  • 6,916
  • 6
  • 38
  • 53
0

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.

Paul Sonier
  • 36,435
  • 3
  • 72
  • 113
0

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.

Lou Franco
  • 83,503
  • 14
  • 127
  • 183
0

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
SwDevMan81
  • 45,922
  • 20
  • 140
  • 177
0

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.

redzedi
  • 1,705
  • 19
  • 27