jeudi 22 juin 2017

Trying to write an "Observer" pattern

Did I correctly implement this pattern. The idea of my example is that there are document and application (app performs an observer role). At beginning each document should attach the observer. Further, document could close all documents except himself by emitting a signal to observer (application).

Requerement - exmaple sholud use event model.

class Application 
{
        std::vector<Document *> docs;
    public:
        void add_document(Document *doc)
        {
            docs.push_back(doc);
        }
    public slots:
        void close_non_active_docs_button_pressed(Document *active_doc)
        {
            for (auto idoc: this->docs)
            {
                if (idoc == active_doc)
                    continue;
                idoc->close();  
            }
        }
}


class Document
{
        Application *app;
    public:
        void attach_to_app(Application *app)
        {
                this->curr_app = app;
                app->add_document(this);
        }

        void close(){...};

    public signals:
        void close_non_active_docs_button_pressed(Document *active_doc);



    ...
        emit close_non_active_docs_button_pressed(this);
}
///CONNECT(...)

Aucun commentaire:

Enregistrer un commentaire