mardi 4 avril 2017

Let 'concrete strategy class' be aware about typedef inside of 'context class'

Here is what my code looks like :

---------------|
|   worker     |      ____________________
|  ---------   |<-----|                  |                 
|  | graph  |  |      |strategy interface|<----- concrete strategy
|  |--------|  |      --------------------
|--------------|

Worker class is the 'context class'. Graph class is an nested class inside of worker, with some boost-graph code, like 'type define', and 'add_edge'. Strategy interface has only one interface 'int DoRoute(int src, int dst)', which return the next hop of a routing.

So, the problem is, how can I let those 'type define' in 'graph class' visible to 'strategy class' for further boost-graph algorithm?

Or I abused 'strategy pattern'? If I abused, what's the elegant way to implement it?

An example code would be :

class woker {
    public :
    class graph {
       struct edge {
           int distance;
       }
       using EdgeProperties = edge;
       //... typedef adjacent_list etc...
    }
    graph g_;
    strategy_interface* p_si_;
    friend strategy_interface;
}

class strategy_interface {
    public :
    stategy_interface(worker& w) : work_(w) {}
    int DoRoute(int, int) = 0;
    protected :
    worker::graph get_graph() {
        return work_.g_;
    }
    private :
    worker& work_;
}

class concrete_strategy {
    public :
    concrete_strategy(worker& w) : strategy_interface(w) {}
    int DoRoute(int s, int d) override {
        auto g = strategy_interface::get_graph();
        // !!! want boost-graph algorithm here
    }
}

Aucun commentaire:

Enregistrer un commentaire