mardi 26 mai 2015

Decouple visualization methods and application

this is my first question on SO, so please bear with me.

We develop an application, which gathers data, and we have methods that let us visualize the data in various ways. With growing number of methods, we decided to separate the application and the visualization methods. I'm wondering what is the best way to accomplish this. I've come up with the following code, which somewhat tries to separate the two, but still ...

Is there a better way to do it?

// forward declaration
class App;

// Interface to all visualization methods
struct Methods {
    virtual void show(App * a) = 0;
};

// Some visualization method
struct Method0 : public Methods {
    void show(App * a) {
        a->getData();
    }
};

class App {
public:
    vector<Methods *> methods;

    void run() {
        // draw all registered methods
        for (auto m : methods)
            m->show(this);
    }

    int getData() {
        // parse and precompute data (time-consuming, thus do it only once)
        // return the required data (not just an int..)
        return 42;
    }
};

void main() {
    App a;

    // register some methods
    a.methods.push_back(new Method0());

    // run the application
    a.run();

    // clean up
    for (auto m : a.methods) delete(m);
}

Aucun commentaire:

Enregistrer un commentaire