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


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!!




Modelling an elevator using Object-Oriented Analysis and Design [closed]

1:



Dealing with nested if then else/nested switch statements
So you want one implementation for some 4 classes, and a different implementation for some other 4 classes? Perhaps the Strategy pattern would work..
How bad is this pattern?
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..
What are some of the things to watch for (pitfalls) while using IOC container?


Pure Virtual Method VS. Function Pointer


Tips for writing fluent interfaces in C# 3

2:



Using the visitor pattern with generics in C#
A).


In the Model View Presenter pattern, can a presenter take and use two different view interfaces at the same time?
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?.


3:


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.


4:


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?.


5:


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..



88 out of 100 based on 68 user ratings 1168 reviews