Hi,
I have following existing scenario.
I have a Validator class containing validate( Command* ) function which validates the Command passed to it.
class Validator
{
public:
validate(Command* cmd)
{
// common validation logic
}
}
I have three classes say WindowsExecute, SolarisExecute and AIXExecute. Member function execute() in SolarisExecute and AIXExecute directly create object of Validator and use the validate( Comman* ) function for validating the Command before executing.
class SolarisExecute
{
public:
execute(Command *cmd)
{
Validator v;
bool valid = v.validate(cmd);
// some processing depending on 'valid'
}
}
class AIXExecute
{
public:
execute(Command *cmd)
{
Validator v;
bool valid = v.validate(cmd);
// some processing depending on 'valid'
}
}
WindowsExecute is completely different and does not have any Command. Instead it need to validate some string data. To do this there is a separate class called WindowsValidator inherited from Validator. WindowsExecute::execute() uses WindowsValidator instead of Validator.
class WindowsValidator : Validator
{
public:
validate(const string &xmlData)
{
// specific validation logic
}
}
class WindowsExecute
{
public:
execute(const string &data)
{
WindowsValidate v;
bool valid = v.validate(data);
// some processing depending on 'valid'
}
}
This is existing code.
Now I need to do some specific validations of Solaris and hence can't use Validator::validate( Command* ). Following the current design, I would need to create new class called SolarisValidator and have my own implementation of validate( Command* ).
I am not comfortable with this approach. Some issues/comments I think:
Validator class would be used only by AIXExecute. Then why have a base class if there is nothing common logic remaining? Simply have three classes SolarisValidator, AIXValidator, WindowsValidator.
Validator::validate( Command* ) unnecessarily gets inherited into WindowsValidate class. Note the signature of WindowsValidate::validate(string) and Validator::validate( Command* ) are different.
I should make Validator::validate( Command* ) virtual if I introduce SolarisValidator::validate( Command* ). It means I am introducing overhead of virtual pointers even though I am not using any dynamic polymorphism. So why not go with #1 above and create three separate classes?
What would be the best solution for this scenario which would also be extensible in future? I am using C++ for implementation.
Thanks in advance.
-GP