开发者

Design: classes with same implementation but different method names

开发者 https://www.devze.com 2023-02-02 08:45 出处:网络
I have multiple classes that have similar implementation for different named methods: class MyClassX { public int MyClassXIntMethod(){}

I have multiple classes that have similar implementation for different named methods:

class MyClassX
{
   public int MyClassXIntMethod(){}
   public string MyClassXStringMethod(){}
}

class MyClassY
{
   public int MyClassYIntMethod(){}
   public string MyClassYStringMethod(){}
}

the methods inside the classes have similar implementation but because the method's names are different (due to 3rd party constraints) i cannot use inheritance.

I'm looking for an elegant solution tha开发者_Python百科t would be better than implementing the same functionality over and over again.


The classic answer IMHO is use the adpater pattern for every 3rd party calling party. Don't apply blindly but see if it is a good fit first.

class MyClassXAdapter
{
   IMyInterface _myImpClass

   public int MyClassXIntMethod(){ return _myImpClass.IntMethod()}
   public string MyClassXStringMethod(){ return _myImpClass.StringMethod() }
}

class MyClassYAdapter
{
   IMyInterface _myImpClass

   public int MyClassYIntMethod(){ return _myImpClass.IntMethod()}
   public string MyClassYStringMethod(){ _myImpClass.StringMethod() }
}

class MyClassImplementation :IMyInterface
{
   public int IntMethod(){}
   public string StringMethod(){}
}


And whats the problem in using composition?

class MyClassY
{
   private MyClassX myclx; 
   public int MyClassYIntMethod()
   {
     return myclx.MyClassXIntMethod();
   }
   public string MyClassYStringMethod(){...Similarly here...}
}


Why not simply create a common super class, and let each "MyClass_" call that common function? You can have a different program signature and still reuse the same codes pieces. Without copy and paste the same code again.

class MyClassX extends MyClassGeneric
{
   public int MyClassXIntMethod(){}
   public string MyClassXStringMethod(){}
}

class MyClassY extends MyClassGeneric
{
   public int MyClassYIntMethod(){ return MyClassIntMethod();}
   public string MyClassYStringMethod(){return MyClassStringMethod();}
}

class MyClassGeneric
{
   protected int MyClassIntMethod(){ /*...... logic .....*/ return 0; }
   protected string MyClassStringMethod(){/*...... logic ....*/return "";}
}


Real world example.

Without "software patternitis". (I apply software patterns, very useful, but, I'm not adicted to them).


collections.hpp

#define pointer void*

class Collection {
protected:
  VIRTUAL bool isEmpty();

  VIRTUAL void Clear();
}

class ArrayBasedCollection: public Collection {
protected:
  int internalInsertFirst(pointer Item);
  int internalInsertLast(pointer Item);

  pointer internalExtractFirst(int Index);
  pointer internalExtractLast(int Index);
}

class Stack: public ArrayBasedCollection {
public:
  OVERLOADED bool isEmpty();
  OVERLOADED void Clear();

  // calls protected "internalInsertFirt"
  void Push(pointer Item);
  // calls protected "internalExtractLast"
  pointer Pop(pointer Item);
}

class Queue: public ArrayBasedCollection {
public:
  OVERLOADED bool isEmpty();
  OVERLOADED void Clear();

  // calls protected "internalInsertFirt"
  void Push(pointer Item);
  // calls protected "internalExtractFirst"
  pointer Pop(pointer Item);
}

Cheers.

0

精彩评论

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