重点:
1.viewmodel 包含model
2.view包含viewmodel,通过驱动viewmodel去控制model的数据和业务逻辑
// Test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <vector>using namespace std;#pragma once
//Model数据处理器
class Model
{
public:void Increace(){count++;}void Decreace(){count--;}int GetCount(){return count;}void SetCount(int num){count = num;}
private:int count{ 10 };
};//ViewModel
class ViewModel
{
public:ViewModel(shared_ptr<Model> model) :m_model(model){}int HandleIncrementPressed(){m_model->Increace();return m_model->GetCount();}int HandleDecrementPressed(){m_model->Decreace();return m_model->GetCount();}int GetInfo(){return m_model->GetCount();}
private:shared_ptr<Model> m_model;
};class View
{
public:View(shared_ptr<ViewModel> vm) :m_viewModel(vm){}void HandleIncrementPressed(){m_viewModel->HandleIncrementPressed();cout << m_viewModel->GetInfo()<<endl;}void HandleDecrementPressed(){m_viewModel->HandleDecrementPressed();cout << m_viewModel->GetInfo() << endl;}
private:shared_ptr<ViewModel> m_viewModel;
};
int main()
{shared_ptr<Model> model= make_shared<Model>();shared_ptr<ViewModel> cont = make_shared<ViewModel>(model);shared_ptr<View> view = make_shared<View>(cont);view->HandleIncrementPressed();view->HandleDecrementPressed();return 0;}