Problem description:
I need to implement 2 classes like following:
class A1 {
common_method1();
common_method2();
foo1();
};
class A2 {
common_method1();
common_method2();
foo2();
};
foo1() and foo2() has different logic.
foo1() and foo2() may have different args and return values.
common methods are the same OR have similar logic.
Target:
To implement factory that is able to generate A1 or A2 objects.
After call to factory::create() use foo1() or foo2() method respectively to the type of the object generated.
Question
How better to implement such logic in C++ C++/CLI?
THANKS!
I think this is definitely a standard inheritance pattern. Create a base class
Parent, which implementscommon_method1andcommon_method2. Create classesA1andA2which inherit fromParent.If you need to do some special casing in one of the
common_method1orcommon_method2methods in eitherA1orA2, make the methodsvirtualinParent.Implement
foo1andfoo2in respectivelyA1andA2.EDIT: If I understand you correctly, you want to create a factory that returns a
Parenttype reference (abstract class). If you want to alwaysfoo1onA1objects, andfoo2onA2objects, simply create a virtual methodbarin theParentinterface, which, overriden inA1, will simply callfoo1, and, overriden inA2, will simply callfoo2.