Eigenes Document View so sinnvoll?



  • Observer.h

    #ifndef OBSERVER_H_INCLUDED
    #define OBSERVER_H_INCLUDED
    
    #include <iostream>
    
    template<typename T> class Observer
    {
        public:
            virtual void update( T* ) = 0;
    };
    #endif // OBSERVER_H_INCLUD
    

    Document.h

    #ifndef DOCUMENT_H_INCLUDED
    #define DOCUMENT_H_INCLUDED
    
    #include "Observer.h"
    #include <vector>
    #include <iostream>
    
    template<typename T>class Document
    {
        public:
            Document(void);
            virtual ~Document();
            virtual void contentChanged() = 0;
            void attach( Observer<T> & );
            void notify();
        private:
            std::vector< Observer<T> *> m_observers;
    };
    
    template<typename T>Document<T>::Document(void)
    {
        std::cout << "ctor Document" << std::endl;
    }
    
    template<typename T>Document<T>::~Document(void)
    {
        std::cout << "dtor Document" << std::endl;
    }
    
    template<typename T> void Document<T>::attach( Observer<T>  &observer)
    {
        m_observers.push_back( &observer );
        std::cout << "attach..." << std::endl;
    }
    
    template<typename T> void Document<T>::notify()
    {
        typename  std::vector< Observer<T> *> ::iterator it;
        for( it = m_observers.begin(); it!= m_observers.end(); it++ )
        {
            (*it)->update( static_cast<T *>( this ) );
        }
    }
    
    #endif // DOCUMENT_H_INCLUDED
    

    Ok, dann mal ein TestDoc

    #ifndef TESTDOCUMENT_H
    #define TESTDOCUMENT_H
    
    #include <wx/object.h>
    
    #include "Document.h"
    
    class TestDocument : public wxObject, public Document<TestDocument>
    {
        public:
            TestDocument();
            virtual TestDocument();
            void getData() const;
            void contentChanged();
        protected:
        private:
    };
    
    #endif // TESTDOCUMENT_H
    

    in der App...

    #ifndef MYTESTAPP_H
    #define MYTESTAPP_H
    
    #include <wx/app.h>
    
    class TestDocument;
    
    class MyTestApp : public wxApp
    {
        public:
            virtual bool OnInit();
            virtual int OnExit();
    
           TestDocument* GetTestDocument() const;
        private:
            TestDocument* m_TestDocument;
    };
    
    DECLARE_APP(MyTestApp)
    
    #endif // MYTESTAPP_H
    

    un der App.cpp

    include "MyTestApp.h"
    
    //(*AppHeaders
    #include "TestMain.h"
    #include <wx/image.h>
    //*)
    #include "TestDocument.h"
    
    IMPLEMENT_APP(MyTestApp);
    
    bool MyTestApp::OnInit()
    {
        TestDocument = new TestDocument();
    
        MyFrame* Frame = NULL;
        //(*AppInitialize
        bool wxsOK = true;
        wxInitAllImageHandlers();
        if ( wxsOK )
        {
        	Frame = new MyFrame(0);
        	Frame->Show();
        	SetTopWindow(Frame);
        }
        //*)
        if ( wxsOK )
        {
            m_TestDocument->attach( *Frame );
            Frame->callAGDoc();
        }
        return wxsOK;
    
    }
    
    int MyTestApp::OnExit()
    {
        delete m_TestDocument;
        return wxApp::OnExit();
    }
    
    TestDocument* MyTestApp::GetAglientDocument() const
    {
        return m_TestDocument;
    }
    

    un das Frame...

    class MyFrame: public wxFrame, public Observer<TestDocument>
    {
        public:
    
            MyFrame(wxWindow* parent,wxWindowID id = -1);
            virtual ~MyFrame();
            virtual void update( TestDocument *document );
            void callAGDoc(void);
    ...
    

    und die cpp...

    MyFrame::MyFrame(wxWindow* parent,wxWindowID id)
    {
       ...
       TestDocument *TestDoc = wxGetApp().GetTestDocument();
       ...
    }
    
    void MyFrame::update( TestDocument *document )
    {
        document->getData();
        std::cout << "MyFrame::update" << std::endl;
    }
    

    Das das Frame die Events aller Controlls bekommt dachte ich den Controller und die View zusammen zu legen, d.h MyFrame ist sowohl View als auch Controller. Sobald jetzt das Frame Daten vom Document erhält, sich diese also geändert haben, wird es informiert (notify). Damit verhält sich das Frame passiv wie eine View. Wenn es aber jetzt Daten anfordern will dann braucht es einen Zeiger auf das Document und kann Daten anfordern. Ok so macht's der Controller.
    So weit wäre das eine Grundlage für Document-View. Aber über den Zeiger kann ich auch Daten lesen. Wozu brauche ich dann notify.
    Irgendwie habe ich das mit dem Document-View noch nicht so richtig kapiert.
    Wo ist da der Haken?


Anmelden zum Antworten