MVC 模式浅析(C++版demo)

网上很少关于C++有关的MVC模式解析,所以最近闲来无事,写了个简单的demo:

// mvc_test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "mvc_model.h"
#include <string>
#include <iostream>

class MVC_View : public MVC_Controller
{
public:
	MVC_View()
	{
		m_CalcModel.setController(this);		
	}

	void onGetResultButtonClick()
	{
		m_CalcModel.setValueA("hello ");
		m_CalcModel.setValueB("world!");
		m_CalcModel.getResult();
	}

	virtual void updateResult(std::string strResult)
	{
		m_strResult = strResult;
		std::cout << m_strResult.c_str() << std::endl;
	}

private:
	MVC_Model m_CalcModel;
	std::string m_strInputA;
	std::string m_strInputB;
	std::string m_strResult;
};


int _tmain(int argc, _TCHAR* argv[])
{
	MVC_View _test;
	
	return 0;
}

mvc_controller.h

#include <string>

class MVC_Controller
{
public:
	virtual void updateResult(std::string strResult) = 0; 
};

mvc_model.h

#pragma once
#include "mvc_controller.h"
#include <string>


class MVC_Model
{
public:
	MVC_Model(void) 
		: m_pCalcController(NULL){

	}
	~MVC_Model(void){

	}
	void setController(MVC_Controller *cb){
		m_pCalcController = cb;
	}

	void setValueA(std::string str){m_strA = str;}
	std::string getValueA(){return m_strA;}

	void setValueB(std::string str){m_strB = str;}
	std::string getValueB(){return m_strB;}

	void setResult(std::string str){m_strResult = str;}
	void getResult(){
		m_strResult = m_strA + m_strB;
		if (m_pCalcController)
		{
			m_pCalcController->updateResult(m_strResult);
		}
	}

private:
	MVC_Controller *m_pCalcController;
	std::string m_strA;
	std::string m_strB;
	std::string m_strResult;
};


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hellokandy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值