0100110010101
0100110010101

Reputation: 6729

Tricky architecture question. 4 Base classes require a bit of extensibility

I have 4 base classes:

class A { virtual SomeMethod () { <code> } }
class A<T> { virtual SomeMethod () { <code> } }

class B { virtual SomeMethod () { <code> } }
class B<T2> { virtual SomeMethod () { <code> } }

Now, i have 4 implementations (each implementation is derived from the corresponding base type).

class Aa : A { override SomeMethod () { <code> } }
class Aa<Tt> : A<T> { override SomeMethod () { <code> } }

class Bb : b { override SomeMethod () { <code> } }
class Bb<Tt2> : B<T2> { override SomeMethod () { <code> } }

Now, i need to add SomeMethod implementation (it should be an override for the one from the base class). The SAME ONE for all of the mentioned derived classes.

What is the best solution? (i will share all my ideas right after the question is solved. since if i put my implementation here, the discussion will most likely go my direction, but i'm not quite sure if i'm right).

Thank you for your great ideas!!

Upvotes: 1

Views: 304

Answers (5)

Matt Howells
Matt Howells

Reputation: 41266

So you want one implementation for some 4 classes, and a different implementation for some other 4 classes? Perhaps the Strategy pattern would work.

interface ISomeMethodStrategy {
    string SomeMethod(string a, string b);
}

class DefaultStrategy : ISomeMethodStrategy {
    public string SomeMethod(string a, string b) { return a + b; }
    public static ISomeMethodStrategy Instance = new DefaultStrategy();
}
class DifferentStrategy : ISomeMethodStrategy {
    public string SomeMethod(string a, string b) { return b + a; }
    public static ISomeMethodStrategy Instance = new DifferentStrategy();
}

class A {
    private ISomeMethodStrategy strategy;
    private string a, b, c;
    public A() : this(DefaultStrategy.Instance) {}
    protected A(ISomeMethodStrategy strategy){
        this.strategy = strategy;
    }
    public void SomeMethod() {
        a = strategy.SomeMethod(b, c);
    }
}

class Aa : A {
    public Aa() : base(DifferentStrategy.Instance) {}
}

I've implemented the pattern here with an interface, but you can do the same with delegates.

Upvotes: 4

nWorx
nWorx

Reputation: 2155

What about interfaces and composition instead of inheritance?

for example you create a new interface

interface myinterface
{
   void doSomethingCool();
}

than a new class which implements it

class interfaceImpl: myinterface
{
    public void doSomethingCool()
    {
         ... some code
    }
}

than with

class A : myinterface
{
  private interfaceImpl interf = MyInterfaceFactory.Build(args);

  public void doSomethingCool()
  {
      interf.doSomethingCool();
  }
}

so you can have the same behaviour in class A, B and all derived classes and you can also override it.

hth

Upvotes: 1

Steve Gilham
Steve Gilham

Reputation: 11277

The question is really asking how to do mix-ins in C#.

You could use a sledgehammer and adopt something like LinFu ; there are other approaches that involve opening up the base definitions to add extra generic parameters (the mix-in object). They all involve working around a deliberate choice in the language design.

Upvotes: 0

Adrian Regan
Adrian Regan

Reputation: 2250

I am assuming that you want to have a single method that is shared (and overridable) between all implementations. The non-generic classes assume a default type that, say int.

Therefore you could create a single originating base class.

public class Origin {
   public virtual void SomeSharedOverrideMethod(int data) { }
}

class A : Origin {

   public void MethodForAImpl() {
       base.SomeSharedOverrideMethod(23);
   }
}

class A<T> : Origin {

   public void MethodForGenericAImpl() {
       base.SomeSharedOverrideMethod(45);
   }
}

class B : Origin
class B<T> : Origin

Is this what you require?

Upvotes: 0

annakata
annakata

Reputation: 75794

A). it would be better if you did post your implementation regardless of whther you think it would skew anything

B). the concept of having a class A and a generic form class A<T> seems really alien. It's like your generic class has an exclusion for one specific case, which means it's less generic than other generics, i.e. wierd.

C). Why would you not just have all 4 base classes inherit from a higher class X if you mean to literally have the same method on each, or from an interface if you merely mean for them to implement this common method. Seems too obvious, have I missed something in your question?

Upvotes: 2

Related Questions