The Most efficient way to start this design

Started by
9 comments, last by Bagpuss 20 years, 6 months ago
Yes the problem is when the object is passed into the validation factory its now a base ptrs. So a few methods to resovle this.

You can use casting to the approraite type or you can use double dispathing. You could create a type factroy but that will use templates and im not even sure how to do that So.

The easiest is to use the double dispatching method.

Youll need to add this into the datatype classes a new function called validate_indirect. Its purpose solely is to recover the type data. Its confusing at first, but you must redfeine this for all dervied child classes, even though the impl doesnt change.

BTW you cant define the funciton impl within the class defintion becuase of a cyclic dependency between valaditaor_base and datatype now.

So predeclare the calss Data_Validator within the header of the datatypes.

And define the functions within the cpp. I impl them iwthin the header for clarity sake.

Oh also ive included the changes the valaidation factory valdiate method. Note its now calling the valdiate indirect function.

struct Datatype_Base{    virtual DATATYPE_SIG    get_type(){ return SIG_INVALID; }              bool            validate            ( void )        {                get_validation_factory()->validate(this);         }        virtual bool    validate_indirect   ( Data_Validator_Base* p_validator )            //Add this function to the base class    {        return p_validator->validate( this );    }    virtual bool    seralize            ( OutputStream& r_stream_out );        virtual bool    seralize            ( InputStream&  r_stream_in  );     };struct Data_Type_Char : public Data_Type_Base{    virtual DATATYPE_SIG    get_type(){ return SIG_CHAR; }             virtual bool    validate_indirect   ( Data_Validator_Base* p_validator )            //Redefine this function for all sub classes, its impl doesnt change                                                                                         //but it passes the correct type to the validator, note cannot inherit from multiple derived data types then..    {        return p_validator->validate( this );    }    virtual bool    seralize            ( OutputStream& r_stream_out );        virtual bool    seralize            ( InputStream&  r_stream_in  );     };bool    Validation_Factory::validate    ( Data_Type_Base* p_data )    {           assert(p_data!=NULL);              DATATYPE_SIG sig = p_data->get_type();         if (m_validators.find(sig) == m_validators.end())      {                   assert(0);                   return false;        }              return p_data->validate_indirect(m_validators[sig]);         //visitor pattern using double dispatch to the correct validator func}


Ive tested this on my side and its all good to go. Sorry for the trouble.

Good Luck!

-ddn

[edited by - ddn3 on October 17, 2003 3:02:39 PM]

[edited by - ddn3 on October 17, 2003 3:05:58 PM]

This topic is closed to new replies.

Advertisement