0

I have an interface:

Interface I
{
   void Update();
}

and 2 classes that implement it, A & B. Also both of the classes use the same exact function in update but B uses different code later on:

Public class A : I
{
   public void Update()
   {
      someFunc();
   }
}

Public class B : I
{
   public void Update().    
   {
      someFunc();
      ....some B code...
   }
}

So I thought move someFunc() to A and let B inherit from A since B does what A does plus something else. Is this design ok because it feels not..or can you advise for something better?

I would like to Leave the interface just like in strategy design pattern since later on there can arrive class C that will implement I interface compelitely differently than A and B.

Greg Oks
  • 2,700
  • 4
  • 35
  • 41

1 Answers1

0

Using inheritance for only code reuse is not a good idea. Only let A inherit if you truly creating a subtype. In your case I would create an Abstract Class that contains the common code.

Interface I
{
   void Update();
}

abstract class AbstractI : I
{
   someFunc() {
      //implementation
   }
}

Public class A : AbstractI 
{
   public void Update()
   {
      someFunc();
   }
}

Public class B : AbstractI 
{
   public void Update().    
   {
      someFunc();
      ....some B code...
   }
}

(never mind the syntax, I made it up - just pseudo code)

Patrick
  • 33,984
  • 10
  • 106
  • 126