I have to create family of objects based on customer type. I have one base abstract class ApplicationRulesFactory, which defines virtual interface. A lot of concrete customer classes inherits from this class.
The problem is that for some customers say CustomerB we do not use the objects Rule2 and Rule3 because the features in the application which are using these objects Rule2 and Rule3 are disabled from the application user interface for that customer, so we are not really needing to instantiate these objects at all.
The simplified code is here, i.e in reality ApplicationRulesFactory has much more virtual methods, and more concrete customer classes that inherits from it :
class ApplicationRulesFactory
{
virtual Rule1* GetRule1() = 0;
virtual Rule2* GetRule2() = 0;
virtual Rule3* GetRule3() = 0;
.....
};
class ACustomerRulesFactory : public ApplicationRulesFactory
{
Rule1* GetRule1()
{
return new ACustomerRule1();
}
Rule2 * GetRule2()
{
return new ACustomerRule2();
}
Rule3* GetRule3()
{
return new ACustomerRule3();
}
};
class BCustomerRulesFactory : public ApplicationRulesFactory
{
Rule1* GetRule1()
{
return new BCustomerRule1();
}
Rule2* GetRule2() // not needed
{
// what to return here ?
}
Rule3* GetRule3() // not needed
{
// what to return here ?
}
};
So how should I go to implement this :
1) Return some default implementation in the base class ApplicationRulesFactory :
class ApplicationRulesFactory
{
virtual Rule1* GetRule1() = 0;
virtual Rule2* GetRule2() { return new Rule2DefaultImpl();}
virtual Rule3* GetRule3() { return new Rule3DefaultIml();}
};
But this seems wrong, to inherit new classes(Rule1DefaultImpl,Rule2DefaultImpl) from Rule1, Rule2, and probably make them with empty implementation just for the purpose of returnig them like default implementation in the ApplicationRulesFactory
2) or in the concrete class return the default implementaion and leave these methods pure virtual in the base class
class BCustomerRulesFactory : public ApplicationRulesFactory
{
Rule1* GetRule1()
{
return new BCustomerRule1();
}
Rule2* GetRule2()
{
return new Rule2DefaultImpl();
}
Rule3* GetRule3()
{
return new Rule3DefaultImpl();
}
};
These solution also seems very ugly to redefine the methods in every concrete customer class although they are not needed.
3) Also I have a feeling that maybe I should not use inheritance like this, cause this violates the IS-A rule for inheritance, cause a significant number of the methods are not applicable to all of the concrete customer classes, but don' t how to go to implement this without inheritance.
Any ideas