设计模式--MVC(C++版)
MVC 模式代表 Model-View-Controller(模型-视图-控制器)模式。这种模式⽤于应⽤程序的分层开发。
Model(模型)-是应⽤程序中⽤于处理应⽤程序数据逻辑的部分。通常模型对象负责在数据库中存取数据。
View(视图) -是应⽤程序中处理数据显⽰的部分。通常视图是依据模型数据创建的。
Controller(控制器) - 是应⽤程序中处理⽤户交互的部分。通常控制器负责从视图读取数据,控制⽤户输⼊,并向模型发送数据。 MVC 分层有助于管理复杂的应⽤程序,因为您可以在⼀个时间内专门关注⼀个⽅⾯。例如,您可以在不依赖业务逻辑的情况下专注于视图设计。同时也让应⽤程序的测试更加容易。
MVC 分层同时也简化了分组开发。不同的开发⼈员可同时开发视图、控制器逻辑和业务逻辑。
以C++代码为例,设计英雄(Hero)对战demo。Hero1对Hero2进⾏攻击,Hero2不断掉⾎,UI同步更新。
主要三个类,分别为Hero(英雄数据类),HeroView(英雄UI类,如⾎条)和HeroController(英雄控制器类,Hero管理类)。
Hero类:
1#pragma once
2
3class Hero
4 {
5public:
6 Hero();
7 ~Hero();
8//be attacked
9void beAttack(int att);
10//is dead?
11bool dead() { return m_nHp == 0; }
12public:
13//set no
14void setNo(std::string no) { m_sNo = no; }
15//get no
16 std::string getNo() { return m_sNo; }
17
18//set name
19void setName(std::string name) { m_sName = name; }
20//get name
21 std::string getName() { return m_sName; }
22
23//get hp
24void setHp(int hp) { m_nHp = hp; }
25//set hp
26int getHp() { return m_nHp; }
27
28//set att
29void setAtt(int att) { m_nAtt = att; }
30//get att
31int getAtt() { return m_nAtt; }
32private:
33
34 std::string m_sName;//姓名
35 std::string m_sNo;//游戏场号码
36
37int m_nHp;//⾎量
38int m_nAtt;//攻击⼒
39 };
Hero.h
1 #include "stdafx.h"
2 #include "Hero.h"
3
4//ctor
5 Hero::Hero()
6 {
7 }
8
9//Destructor
10 Hero::~Hero()
11 {
12
13 }
14
15void Hero::beAttack(int att)
16 {
17if (att <= 0) throw"Att Number <= 0";//safe check
18 m_nHp -= att;
19 m_nHp = m_nHp < 0 ? 0 : m_nHp;//safe check
20 }
Hero.cpp
HeroView类:
1#pragma once
2class HeroView
3 {
4public:
5 HeroView();
6 ~HeroView();
7
8//show hero UI data;
9void show(string no, string name,int att, int hp);
10//show hero dead UI;
11void dead(string no, string name);
12//show heor winned UI;
13void winned(string no, string name);
14private:
15//Hero* m_hero;
16 };
HeroView.h
1 #include "stdafx.h"
2 #include "HeroView.h"
3
4
5
6 HeroView::HeroView()
7 {
8 }
9
10 HeroView::~HeroView()
11 {
12 }
13
14void HeroView::show(string no,string name, int att, int hp)
15 {
16 cout << "Hero info:"<<"(no:"<<no<<",name:"<<name<<",att:"<<att<<",hp:"<<hp<<")" << endl;
17 }
18
19void HeroView::dead(string no, string name)
20 {
21 cout << "Hero Dead:" << "(no:" << no << ",name:"<<name << ")" << endl;
22 }
23
24void HeroView::winned(string no, string name)
25 {
26 cout << "Hero Winned:" << "(no:" << no << ",name:" << name << ")" << endl;
27
28 }
HeroView.cpp
HeroController类:
1#pragma once
2class HeroController
3 {
4public:
5 HeroController(string no, string name);
6 ~HeroController();
7void setHeroHp(int hp);//set hero hp
8void setHeroAtt(int att);//set hero att
9void show();//show hero info
10void beAttack(int att);//be attacked by hero
11void dead();//dead
12void winned();//winned
13public :
14 Hero* getHero() { return m_hero; }//get hero
15
16private:
17 Hero * m_hero;//hero
18 HeroView * m_heroView;//hero view
19 };
HeroController.h
1 #include "stdafx.h"
2 #include "HeroController.h"
3
4
5 HeroController::HeroController(string no, string name)
6 {
7 m_heroView = new HeroView();
8 m_hero = new Hero();
9 m_hero->setNo(no);
10 m_hero->setName(name);
11 }
12
13
14 HeroController::~HeroController()
15 {
16 }
17
18
19void HeroController::setHeroHp(int hp)
20 {
21 m_hero->setHp(hp);
22 }
23
24void HeroController::setHeroAtt(int att)
25 {
26 m_hero->setAtt(att);
27 }
28
29void HeroController::show()
30 {
31 m_heroView->show(m_hero->getNo(), m_hero->getName(),m_hero->getAtt(), m_hero->getHp());
32 }
33
34void HeroController::beAttack(int att)
35 {
36 m_hero->beAttack(att);
37 }
38
39void HeroController::dead()
40 {
41 m_heroView->dead(m_hero->getNo(),m_hero->getName());
42 }
43
44void HeroController::winned()
mvc的controller45 {
46 m_heroView->winned(m_hero->getNo(), m_hero->getName());
47 }
HeroController.cpp
main() 测试:
1// ConsoleApplication_C++1.cpp: 主项⽬⽂件。
2
3 #include "stdafx.h"
4
5using namespace System;
6
7int main()
8 {
9//初始化⼀个英雄的控制器
10 HeroController* controller = new HeroController("2017","孟栋");
11//设置⾎量100
12 controller->setHeroHp(100);
13 controller->setHeroAtt(40);
14//显⽰⼀下⾎量
15 controller->show();
16
17//初始化第⼆个英雄的控制器
18 HeroController* controller2 = new HeroController("2016", "⿊魔王");
19//设置⾎量100
20 controller2->setHeroHp(200);
21//设置攻击⼒20
22 controller2->setHeroAtt(20);
23//显⽰⼀下⾎量
24 controller2->show();
25
26//hero1 attack hero2
27 controller2->beAttack(controller->getHero()->getAtt());
28//ui更新
29 controller2->show();
30
31//如果没有dead,就⼀直攻击
32while (!controller2->getHero()->dead())
33 {
34//hero1 attack hero2
35 controller2->beAttack(controller->getHero()->getAtt()); 36//ui更新
37 controller2->show();
38 }
39
40 controller2->dead();
41 controller->show();
42 controller->winned();
43
44
45
46return0;
47 }
main
控制台输出如下:
1 Hero info : (no:2017, name : 孟栋, att : 40, hp : 100)
2 Hero info : (no : 2016, name : ⿊魔王, att : 20, hp : 200)
3 Hero info : (no : 2016, name : ⿊魔王, att : 20, hp : 160)
4 Hero info : (no : 2016, name : ⿊魔王, att : 20, hp : 120)
5 Hero info : (no : 2016, name : ⿊魔王, att : 20, hp : 80)
6 Hero info : (no : 2016, name : ⿊魔王, att : 20, hp : 40)
7 Hero info : (no : 2016, name : ⿊魔王, att : 20, hp : 0)
8 Hero Dead : (no : 2016, name : ⿊魔王)
9 Hero info : (no : 2017, name : 孟栋, att : 40, hp : 100)
10 Hero Winned : (no : 2017, name : 孟栋)
11
View Code
多加点评,谢谢!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论