reference data class member visitor pattern

88 views Asked by At

I have a container class that has a plain old datatype called DataStore. This data store will be passed to visitor.They share a single copy since any updated performed by the container are to be seen by visitor.

class xyz
{
    //Plain Old Datatype
    typedef struct DataStore
    {
        //add any new required data by any test here
        bool    _detectorConnectionStatus;
        DataStore():_detectorConnectionStatus(false){}
    }DataStore;

    DataStore _dataStore;

    typedef struct visitorData
    {
        DataStore& dataStore;
        visitorData(DataStore data):dataStore(data){}
    }visitorData;

    //data to be sent to visitor
    visitorData _visitorData;
};

xyz::xyz():_visitorData(_dataStore)
{
}

class IVisitor
{
private:
struct DataStore* dataStore;
public:
//get the data,no check for validity of data performed
 void visit(struct DataStore& dataStore){};
//process the data
virtual void process() = 0;
};

The code compiles but is there a better/cleaner way to do it?

1

There are 1 answers

3
Spanky On BEST ANSWER

Well since its a design question, I thought of it in a different way. Not knowing your limitations fully, here is my suggestion:

  1. Make DataStore and visitorData separate class with proper getter and setter methods. Do away with the old datatype if u can.
  2. Convert the xyz class to a singleton and make it an accessor class with proper methods.

  3. So your utility class derived from IVisitor will get the singleton instance and singleton will in turn get the data for you.

Thus it will implement high level of abstraction as expected by OOD.

I have just given a skeleton of an idea and you will need to work out the details.

Hope this helps