Skip to content.

Sections
Personal tools
You are here: Home » ダウンロード » ThreadJack用 » observable

Document Actions
#include <iostream>
#include <string>
#include <tjutil.h>
 

class Model : public TJObservable {
public:
    Model() : myString("") { }

    void setString(string str) {
        myString = str;
        setChanged();
    }
    string getString() const {
         return myString;
    }

private:
    string myString;
};

class View : public TJObserver {
    virtual void update(TJObservable& o, void*) {
        Model& m = static_cast<Model&>(o);
        string str = m.getString();
        cout << "###View::update" << endl;
        cout << "   Model's str = " << str << endl;
    }
};
 

int main() {
    Model* m = new Model;
    View*  v = new View;

    m->addObserver(v);

    cout << "## Input string.(finish -> ^D)" << endl;
    string answer;
    while (cin >> answer) {
        m->setString(answer);
        m->notifyObservers();
    }

    m->removeObservers();
    delete v;
    delete m;

    return 0;
}