Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat would be the correct design approach?
    primarykey
    data
    text
    <p>I have following existing scenario. </p> <p>I have a Validator class containing validate( Command* ) function which validates the Command passed to it.</p> <pre><code>class Validator { public: validate(Command* cmd) { // common validation logic } } </code></pre> <p>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.</p> <pre><code>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' } } </code></pre> <p>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.</p> <pre><code>class WindowsValidator : Validator { public: validate(const string &amp;xmlData) { // specific validation logic } } class WindowsExecute { public: execute(const string &amp;data) { WindowsValidate v; bool valid = v.validate(data); // some processing depending on 'valid' } } </code></pre> <p>This is existing code. </p> <p>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* ).</p> <p>I am not comfortable with this approach. Some issues/comments I think:</p> <ol> <li><p>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.</p></li> <li><p>Validator::validate( Command* ) unnecessarily gets inherited into WindowsValidate class. Note the signature of WindowsValidate::validate(string) and Validator::validate( Command* ) are different.</p></li> <li><p>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?</p></li> </ol> <p>What would be the best solution for this scenario which would also be extensible in future? I am using C++ for implementation.</p> <p>Thanks in advance.</p> <p>-GP</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload