MVC 模式的c++例子与理解

MVC 模式的c++例子与理解_第1张图片

文章目录

    • 主动与被动模式
    • CMakeLists.txt
    • Client.h
    • Model.h
    • View.h
    • main.cpp

主动与被动模式

待写

CMakeLists.txt

https://github.com/tonixtom/BigTalkDesignPatternCppTest.git

cmake_minimum_required(VERSION 3.20)
project(mvc_test)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Debug)

set(Srcs
        main.cpp
        internal/Controller.cpp
        internal/View.cpp
        internal/CreditsView.cpp
        internal/VersionView.cpp
        internal/TitleView.cpp)

set(Hdrs
        internal/Client.h
        internal/Controller.h
        internal/Model.h
        internal/Observer.h
        internal/Observable.h
        internal/View.h
        internal/TitleView.h
        internal/VersionView.h
        internal/CreditsView.h)

add_executable(${PROJECT_NAME} ${Srcs} ${Hdrs})

Client.h

#ifndef MVC_TEST_CLIENT_H
#define MVC_TEST_CLIENT_H
#include "Model.h"
#include "View.h"
#include "Controller.h"

//class Application, get Model, Views and Controller together
class Client
{
public:
    Client()
    {
        views.setModel(model);
        controller.setModel(model);
        model.notifyObservers();
    }
    void run(){controller.MessageLoop();}

private:
    Model model;
    Views views;
    Controller controller;
};

#endif //MVC_TEST_CLIENT_H

Model.h

#ifndef MVC_TEST_MODEL_H
#define MVC_TEST_MODEL_H

#include 
#include 
#include "Observer.h"
#include "Observable.h"

using namespace std;

class Model: public Observable
{
public:
    //conclassor
    Model() :
            title_caption("Title: "),
            version_caption("Version: "),
            credits_caption("Credits: "),
            title("Simple Model-View-Controller Implementation"),
            version("0.2"),
            credits("(put your name here)")
    { }

    //getCredits_Caption, getTitle_Caption, getVersion_Caption
    string getCredits_Caption(){return credits_caption;}
    string getTitle_Caption(){return title_caption;}
    string getVersion_Caption(){return version_caption;}

    //getCredits, getTitle, getVersion
    string getCredits(){return credits;}
    string getTitle(){return title;}
    string getVersion(){return version;}

    //setCredits, setTitle, setVersion
    void setCredits(string a){credits=a;notifyObservers();}
    void setTitle(string a){title=a;notifyObservers();}
    void setVersion(string a){version=a;notifyObservers();}

private:
    //data members title_caption, version_caption, credits_caption
    string title_caption;
    string version_caption;
    string credits_caption;

    //data members title, version, credits
    string title;
    string version;
    string credits;
};
#endif //MVC_TEST_MODEL_H

View.h

#ifndef MVC_TEST_VIEW_H
#define MVC_TEST_VIEW_H

#include 
#include "Observable.h"
#include "Observer.h"
#include "TitleView.h"
#include "VersionView.h"
#include "CreditsView.h"

using namespace std;

//构造视图,将所有观察者打包到另一个观察者中
class Views: public Observer
{
public:
    //setModel
    void setModel(Observable &a)
    {
        a.addObserver(&titleview);
        a.addObserver(&versionview);
        a.addObserver(&creditsview);
        a.addObserver(this);
    }
    //update
    virtual void update(void* a) override;

private:
    //data members titleview, versionview, creditsview
    TitleView titleview;
    VersionView versionview;
    CreditsView creditsview;
};
#endif //MVC_TEST_VIEW_H

main.cpp

#include 
#include "./internal/Client.h"

//Model 字符串数据和设置和获取数据的方法
//控制器会触发模型更新,并且还提供最新信息做Views.

int main()
{
    Client *tom = new Client();
    tom->run();
    return 0;
}

你可能感兴趣的:(软件工程,mvc,c++,开发语言)