用C++實(shí)現(xiàn)MVVM

MVVM

文章最先發(fā)表于用C++實(shí)現(xiàn)MVVM.
歡迎關(guān)注博客.

用C++實(shí)現(xiàn)MVVM

MVVM(Model-View-ViewModel)是現(xiàn)在比較流行的GUI程序的框架巡李。

通過(guò)代碼的編寫(xiě)抚笔,我談?wù)勎覍?duì)于MVVM的理解。

整體代碼的sample在Graphics Editor可以看到侨拦。

GUI庫(kù)使用了QT5.9殊橙,功能代碼主要使用了OpenCV庫(kù)。

后面一些功能的編寫(xiě)不是我寫(xiě)的狱从,所以代碼風(fēng)格可能有些不和諧膨蛮,這里主要集中精力于整個(gè)框架的實(shí)現(xiàn),忽略其各項(xiàng)功能的實(shí)現(xiàn)季研。

如果有任何理解不對(duì)的地方敞葛,歡迎您批評(píng)指出。

MVVM

阮一峰的"MVC与涡,MVP 和 MVVM 的圖示"中惹谐, 介紹了三個(gè)架構(gòu)之間的區(qū)別。

總結(jié)來(lái)說(shuō)驼卖,就是在Model氨肌,View,ViewModel三個(gè)模塊之間酌畜,View與ViewModel之間的數(shù)據(jù)通過(guò)雙向綁定進(jìn)行聯(lián)系怎囚,View與Model之間不產(chǎn)生聯(lián)系,ViewModel操作Model進(jìn)行數(shù)據(jù)處理桥胞。

(這里實(shí)際寫(xiě)代碼的時(shí)候好像跟阮老師所說(shuō)的有一些區(qū)別:按照阮老師所說(shuō)恳守,應(yīng)該是ViewModel在功能上相當(dāng)于MVP模式中的Presenter考婴,所有邏輯都部署在這里,實(shí)際上寫(xiě)的時(shí)候應(yīng)該是大部分邏輯都部署在Model層進(jìn)行數(shù)據(jù)操作井誉,然后通知ViewModel和View進(jìn)行更新蕉扮,不知道是否是在我的理解中出現(xiàn)問(wèn)題……)

項(xiàng)目目錄

.
├── app.cpp
├── app.h
├── command.cpp
├── command.h
├── Commands
│   ├── alter_bright_command.cpp
│   ├── alter_bright_command.h
│   ├── crop_command.cpp
│   ├── crop_command.h
│   ├── detect_face_command.cpp
│   ├── detect_face_command.h
│   ├── filter_command.cpp
│   ├── filter_command.h
│   ├── open_file_command.cpp
│   ├── open_file_command.h
│   ├── reset_command.cpp
│   ├── reset_command.h
│   ├── rotate_command.cpp
│   ├── rotate_command.h
│   ├── save_bmp_command.cpp
│   ├── save_bmp_command.h
│   ├── save_file_command.cpp
│   └── save_file_command.h
├── common.cpp
├── common.h
├── GraphicsEditor.pro
├── GraphicsEditor.pro.user
├── LICENSE
├── main.cpp
├── model.cpp
├── model.h
├── MyView.cpp
├── MyView.h
├── notification.cpp
├── notification.h
├── parameters.cpp
├── parameters.h
├── README.md
├── test.pro
├── test.pro.user
├── view.cpp
├── view.h
├── viewmodel.cpp
├── viewmodel.h
└── view.ui

項(xiàng)目架構(gòu)介紹

各個(gè)類以及之間關(guān)系如下:

App

class App
{
private:
    std::shared_ptr<View> view;
    std::shared_ptr<Model> model;
    std::shared_ptr<ViewModel> viewmodel;

public:
    App();
    void run();
};

在構(gòu)造函數(shù)中,對(duì)各項(xiàng)需要初始化和綁定的數(shù)據(jù)進(jìn)行綁定:


App::App():view(new View),model(new Model), viewmodel(new ViewModel)
{

    viewmodel->bind(model);

    view->set_img(viewmodel->get());

    view->set_open_file_command(viewmodel->get_open_file_command());
    view->set_alter_bright_command(viewmodel->get_alter_bright_command());
    view->set_filter_rem_command(viewmodel->get_filter_rem_command());
    view->set_reset_command(viewmodel->get_reset_command());
    view->set_detect_face_command(viewmodel->get_detect_face_command());
    view->set_save_file_command(viewmodel->get_save_file_command());
    view->set_save_bmp_file_command(viewmodel->get_save_bmp_file_command());
    view->set_rotate_command(viewmodel->get_rotate_command());
    view->set_crop_command(viewmodel->get_crop_command());

    viewmodel->set_update_view_notification(view->get_update_view_notification());
    model->set_update_display_data_notification(viewmodel->get_update_display_data_notification());

}

View

class View : public QMainWindow
{
    Q_OBJECT

public:
    explicit View(QWidget *parent = 0);
    ~View();

    void update();
    void set_img(std::shared_ptr<QImage> image);
    void set_open_file_command(std::shared_ptr<Command>);
    void set_alter_bright_command(std::shared_ptr<Command>);
    void set_filter_rem_command(std::shared_ptr<Command>);
    void set_reset_command(std::shared_ptr<Command>);
    void set_detect_face_command(std::shared_ptr<Command>);
    void set_save_file_command(std::shared_ptr<Command>);
    void set_save_bmp_file_command(std::shared_ptr<Command>);
    void set_rotate_command(std::shared_ptr<Command>);
    void set_crop_command(std::shared_ptr<Command>);
    std::shared_ptr<Notification> get_update_view_notification();

private slots:
    void on_button_open_clicked();
    void on_brightSlider_valueChanged(int value);
    void on_contrastSlider_valueChanged(int value);
    void on_filter_1_clicked();
    void on_reset_clicked();
    void on_actionOpen_File_triggered();
    void on_button_detect_face_clicked();
    void on_actionSave_triggered();
    void on_action_bmp_triggered();
    void on_action_png_triggered();
    void on_action_jpeg_triggered();
    void on_rotateSlider_valueChanged(int value);

private:
    Ui::View *ui;
    MyView* canvas;
    std::shared_ptr<QImage> q_image;
    std::shared_ptr<Command> open_file_command;
    std::shared_ptr<Command> alter_bright_command;
    std::shared_ptr<Command> filter_rem_command;
    std::shared_ptr<Command> reset_command;
    std::shared_ptr<Command> detect_face_command;
    std::shared_ptr<Command> save_file_command;
    std::shared_ptr<Command> save_bmp_file_command;
    std::shared_ptr<Command> rotate_command;
    std::shared_ptr<Command> crop_command;

    std::shared_ptr<Notification> update_view_notification;
};

本身提供一個(gè)用于更新的notification, 并提供get()方法交給ViewModel層進(jìn)行綁定颗圣,如此可以實(shí)現(xiàn)ViewModel通知View進(jìn)行更新喳钟。

同時(shí),本身提供很多Command的成員變量在岂,這些變量本省并不屬于View層奔则,本身屬于ViewModel層,并在ViewModel層提供get方法給View層進(jìn)行set綁定蔽午,這樣就實(shí)現(xiàn)了View發(fā)送commandViewModel層易茬,View就可以在不知道Command具體派生類的情況下寫(xiě)代碼。

ViewModel

class ViewModel
{
private:
    std::shared_ptr<QImage> q_image;
    std::shared_ptr<Model> model;


    std::shared_ptr<Command> open_file_command;
    std::shared_ptr<Command> alter_bright_command;
    std::shared_ptr<Command> filter_rem_command;
    std::shared_ptr<Command> reset_command;
    std::shared_ptr<Command> detect_face_command;
    std::shared_ptr<Command> save_file_command;
    std::shared_ptr<Command> save_bmp_file_command;
    std::shared_ptr<Command> rotate_command;
    std::shared_ptr<Command> crop_command;

    std::shared_ptr<Notification> update_display_data_notification;

    std::shared_ptr<Notification> update_view_notification;

public:
    ViewModel();
    void bind(std::shared_ptr<Model> model);
    void exec_open_file_command(std::string path);
    void exec_alter_bright_command(int nBright, int nContrast);
    void exec_filter_rem_command();
    void exec_reset_command();
    void exec_detect_face_command();
    void exec_save_file_command(std::string path);
    void exec_save_bmp_file_command(std::string path);
    void exec_rotate_command(int angle);
    void exec_crop_command(double x_s, double y_s, double x_e, double y_e);

    void set_update_view_notification(std::shared_ptr<Notification> notification);

    std::shared_ptr<Command> get_open_file_command();
    std::shared_ptr<Command> get_alter_bright_command();
    std::shared_ptr<Command> get_filter_rem_command();
    std::shared_ptr<Command> get_reset_command();
    std::shared_ptr<Command> get_detect_face_command();
    std::shared_ptr<Command> get_save_file_command();
    std::shared_ptr<Command> get_save_bmp_file_command();
    std::shared_ptr<Command> get_rotate_command();
    std::shared_ptr<Command> get_crop_command();

    std::shared_ptr<Notification> get_update_display_data_notification();
    std::shared_ptr<QImage> get();

    void notified();
};

View層之間的通信在之前已經(jīng)講過(guò)及老,在構(gòu)造函數(shù)中初始化具體的命令抽莱,然后get交給Viewset進(jìn)行綁定。這其中有一個(gè)向基類指針的轉(zhuǎn)換骄恶,我是這么寫(xiě)的:

 open_file_command = std::static_pointer_cast<Command, OpenFileCommand>(std::shared_ptr<OpenFileCommand> (new OpenFileCommand(std::shared_ptr<ViewModel>(this))));

然后與Model間的通信沒(méi)有通過(guò)Command食铐,而是直接獲得一個(gè)Model的指針,調(diào)用它的功能函數(shù)即可僧鲁。

Model


class Model
{
private:
     cv::Mat image;
     std::shared_ptr<Notification> update_display_data_notification;
public:
    Model(){}
    void set_update_display_data_notification(std::shared_ptr<Notification> notification);
    void open_file(std::string path);
    cv::Mat& get();
    cv::Mat& getOrigin();
    void notify();
    void save_file(std::string path);
    void save_bmp_file(std::string path);

    void alterBrightAndContrast(int nbright, int nContrast);
    void detect_face();
    void filterReminiscence(); //Filter No.1
    void reset();
    void rotate(double angle);
    void crop(int x1, int y1, int x2, int y2);
};


Model層本身又一個(gè)set一個(gè)notification的接口虐呻,這個(gè)notification用于通知ViewModel進(jìn)行更新數(shù)據(jù)。

其他的就是針對(duì)數(shù)據(jù)的一些功能代碼寞秃。

Command

本身可以寫(xiě)為純虛類斟叼,我是寫(xiě)了一個(gè)成員變量是一個(gè)基類參數(shù)的指針,然后所有具體的command都是派生于此春寿,提供exec()方法朗涩。


class Command
{
protected:
    std::shared_ptr<Parameters> params;
public:
    Command();
    void set_parameters(std::shared_ptr<Parameters> parameters){
        params = parameters;
    }
    virtual void exec() = 0;
};

Notification


class Notification
{
public:
    Notification();
    virtual void exec() = 0;
};



class UpdateDisplayDataNotification: public Notification{
private:
    std::shared_ptr<ViewModel> viewmodel;
public:
    UpdateDisplayDataNotification(std::shared_ptr<ViewModel> vm):viewmodel(vm){}
    void exec(){
        viewmodel->notified();
    }
};


class UpdateViewNotification: public Notification{
private:
    std::shared_ptr<View> view;
public:
    UpdateViewNotification(std::shared_ptr<View> v):view(v){}
    void exec(){
        view->update();
    }
};

Parameters


class Parameters
{
public:
    Parameters();
};


class PathParameters: public Parameters{
private:
    std::string path;
public:
    PathParameters(std::string _path):path(_path){
    }
    std::string get_path(){
        return path;
    }
};

PathParameters為例表示了一般的新的參數(shù)的派生方法。

common

實(shí)現(xiàn)了cv::MatQImage之間的轉(zhuǎn)換代碼绑改。

整體流程

View層進(jìn)行操作之后谢床,會(huì)觸發(fā)對(duì)應(yīng)槽函數(shù),該槽函數(shù)會(huì)準(zhǔn)備好參數(shù)Parameter交給對(duì)應(yīng)的Command绢淀,然后執(zhí)行exec()這個(gè)command萤悴,exec會(huì)解出參數(shù)交給ViewModel層,ViewModel調(diào)用Model里對(duì)應(yīng)的方法皆的,進(jìn)行數(shù)據(jù)操作覆履,Model操作完之后會(huì)通知ViewModel更新顯示數(shù)據(jù),ViewModel會(huì)通知View刷新顯示。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末硝全,一起剝皮案震驚了整個(gè)濱河市栖雾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌伟众,老刑警劉巖析藕,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異凳厢,居然都是意外死亡账胧,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)先紫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)治泥,“玉大人,你說(shuō)我怎么就攤上這事遮精【蛹校” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵本冲,是天一觀的道長(zhǎng)准脂。 經(jīng)常有香客問(wèn)我,道長(zhǎng)檬洞,這世上最難降的妖魔是什么狸膏? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮疮胖,結(jié)果婚禮上环戈,老公的妹妹穿的比我還像新娘闷板。我一直安慰自己澎灸,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布遮晚。 她就那樣靜靜地躺著性昭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪县遣。 梳的紋絲不亂的頭發(fā)上糜颠,一...
    開(kāi)封第一講書(shū)人閱讀 52,328評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音萧求,去河邊找鬼其兴。 笑死,一個(gè)胖子當(dāng)著我的面吹牛夸政,可吹牛的內(nèi)容都是我干的元旬。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼匀归!你這毒婦竟也來(lái)了坑资?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤穆端,失蹤者是張志新(化名)和其女友劉穎袱贮,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體体啰,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡攒巍,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了荒勇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片窑业。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖枕屉,靈堂內(nèi)的尸體忽然破棺而出常柄,到底是詐尸還是另有隱情,我是刑警寧澤搀擂,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布西潘,位于F島的核電站,受9級(jí)特大地震影響哨颂,放射性物質(zhì)發(fā)生泄漏喷市。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一威恼、第九天 我趴在偏房一處隱蔽的房頂上張望品姓。 院中可真熱鬧,春花似錦箫措、人聲如沸腹备。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)植酥。三九已至,卻和暖如春弦牡,著一層夾襖步出監(jiān)牢的瞬間友驮,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工驾锰, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留卸留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓椭豫,卻偏偏與公主長(zhǎng)得像耻瑟,于是被迫代替她去往敵國(guó)和親买喧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容