#include<iostream>
#include<vector> //get namespace related stuff
using std::cin;
using std::cout;
using std::endl;
using std::flush;
using std::string;
using std::vector; //struct Observer, modeled after java.utils.Observer
struct Observer
/*
* AK: This could be a template (C++) or generic (Java 5),
* however the original Smalltalk MVC didn't do that.
*/
{
//update
virtual void update(void*)=;
}; //struct Observable, modeled after java.utils.Observable
struct Observable
{
//observers
vector<Observer*>observers; //addObserver
void addObserver(Observer*a){observers.push_back(a);} //notifyObservers
void notifyObservers()
{
for (vector<Observer*>::const_iterator observer_iterator=observers.begin();observer_iterator!=observers.end();observer_iterator++)
(*observer_iterator)->update(this);
} /*
AK: If you had a method which takes an extra "ARG" argument like this
notifyObservers(void* ARG), you can pass that arg to each Observer via
the call (*observer_iterator)->update(this,ARG); This can significantly increase your View's reusablity down the track.
I'll explain why below in the View.
*/ }; //struct Model, contains string-data and methods to set and get the data
struct Model:Observable
{
//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; //constructor
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();}
/*
* AK notifyObservers(a) for credit, title and version.
* All as per discussion in View and Observer *
*/
}; /*
AK:
Great stuff ;-) This satisfies a major principle of the MVC
architecture, the separation of model and view. The model now has NO View material in it, this model can now be used in
other applications.
You can use it with command line apps (batch, testing, reports, ...),
web, gui, etc. Mind you "MVC with Passive Model" is a variation of MVC where the model
doesn't get even involved with the Observer pattern. In that case the Controller would trigger a model update *and it* could
also supply the latest info do the Views. This is a fairly common MVC
variation, especially with we apps.
*/ //struct TitleView, specialized Observer
struct TitleView:Observer
{
/*
* AK:
* I like to get a reference to the model via a constructor to avoid
* a static_cast in update and to avoid creating zombie objects.
*
* A zombie object is instantiated but is unusable because it
* is missing vital elements. Dangerous. Getting model via the
* constructor solves this problem. Model model;
// Cons.
TitleView (Model* m) .... RE-USABILITY.
Some views are better off working with the full Model, yet others are
better off being dumber. I like to have two kinds of Views. Those that work with full Model (A)
and those that only work with a limited more abstract data type (B). Type A.
Complex application specific views are better off getting the full
model, they can then just pick and choose what they need from the full
model without missing something all the time. Convenient. Type B.
These only require abstract or generic data types. Consider a PieChartView, it doesn't really need to know about the full
Model of a particular application, it can get by with just float
*values[] or vector<float>; By avoiding Model you can then reuse PieChartView in other applications
with different models. For this to be possible you must use the 2 argument version of
notifyObservers. See comments on Observer class. See my Java example NameView. That view only knows about a String, not
the full Model.
*/ //update
void update(void*a)
/*
*AK:void update(void*a, void*arg) is often better. As per discussion
above.
*/
{
cout<<static_cast<Model*>(a)->getTitle_Caption();
cout<<static_cast<Model*>(a)->getTitle();
cout<<endl;
}
}; //struct VersionView, specialized Observer
struct VersionView:Observer
{ //update
void update(void*a)
{
cout<<static_cast<Model*>(a)->getVersion_Caption();
cout<<static_cast<Model*>(a)->getVersion();
cout<<endl;
}
}; //struct CreditsView, specialized Observer
struct CreditsView:Observer
{ //update
void update(void*a)
{
cout<<static_cast<Model*>(a)->getCredits_Caption();
cout<<static_cast<Model*>(a)->getCredits();
cout<<endl;
}
}; //struct Views, pack all Observers together in yet another Observer
struct Views:Observer
{
//data members titleview, versionview, creditsview
TitleView titleview;
VersionView versionview;
CreditsView creditsview;
/*
* AK:
* Views are often hierarchical and composed of other Views. See
Composite pattern.
* vector<View*> views;
*
* Views often manage (create and own) a Controller.
*
* Views may include their own Controller code (Delegate).
*
*/
//setModel
void setModel(Observable&a)
{
a.addObserver(&titleview);
a.addObserver(&versionview);
a.addObserver(&creditsview);
a.addObserver(this);
} //update
void update(void*a)
{
cout<<"_____________________________";
cout<<"\nType t to edit Title, ";
cout<<"v to edit Version, ";
cout<<"c to edit Credits. ";
cout<<"Type q to quit./n>>";
}
}; //struct Controller, wait for keystroke and change Model
struct Controller
/*
* AK: Controller can also be an Observer.
*
* There is much to say about Controller but IMHO we should defer
* that to another version.
*/
{
//data member model
Model*model; //setModel
void setModel(Model&a){model=&a;} //MessageLoop
void MessageLoop()
{
char c=' ';
string s;
while(c!='q')
{
cin>>c;
cin.ignore(,'\n');
cin.clear();
switch(c)
{
case 'c':
case 't':
case 'v':
getline(cin,s);
break;
}
switch(c)
{
case 'c':model->setCredits(s);break;
case 't':model->setTitle(s);break;
case 'v':model->setVersion(s);break;
}
}
}
}; //struct Application, get Model, Views and Controller together
struct Application
{ //data member model
Model model; //data member views
Views views; //data member controller
Controller controller; //constructor
Application()
{
views.setModel(model);
controller.setModel(model);
model.notifyObservers();
} //run
void run(){controller.MessageLoop();}
}; //main
int main()
{
Application().run();
return ;
}

一个很小的C++写的MVC的例子的更多相关文章

  1. 【生产问题】记还原一个很小的BAK文件,但却花了很长时间,分析过程

    [生产问题]还原一个很小的BAK文件,但却花了很长时间? 关键词:备份时事务日志太大会发生什么?还原时,事务日志太大会怎么办? 1.前提: [1.1]原库数据已经丢失,只有这个bak了 [1.2]ba ...

  2. 【mysql】一个很小但很影响速度的地方

    如果要插入一大批数据,千万不要一条一条的execute, commit.而应该是先全部execute,最后统一commit!!! 千万注意,时间差距还是很大的!! 正确示范:快 ): sql = &q ...

  3. jquery学习心得:一个很好的css和js函数调用的例子

    统一目录下的资源结构图: <html><head> <link rel="stylesheet" href="gallery.css&quo ...

  4. 手把手教你写一个RN小程序!

    时间过得真快,眨眼已经快3年了! 1.我的第一个App 还记得我14年初写的第一个iOS小程序,当时是给别人写的一个单机的相册,也是我开发的第一个完整的app,虽然功能挺少,但是耐不住心中的激动啊,现 ...

  5. 有一个很大的整数list,需要求这个list中所有整数的和,写一个可以充分利用多核CPU的代码,来计算结果(转)

    引用 前几天在网上看到一个淘宝的面试题:有一个很大的整数list,需要求这个list中所有整数的和,写一个可以充分利用多核CPU的代码,来计算结果.一:分析题目 从题中可以看到“很大的List”以及“ ...

  6. 很小的一个函数执行时间调试器Timer

    对于函数的执行性能(这里主要考虑执行时间,所耗内存暂不考虑),这里写了一个简单的类Timer,用于量化函数执行所耗时间. 整体思路很简单,就是new Date()的时间差值.我仅仅了做了一层简单的封装 ...

  7. 「小程序JAVA实战」 小程序手写属于自己的第一个demo(六)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-06/ 自己尝试的写一个小demo,用到自定义样式,自定义底部导航,页面之间的跳转等小功能.官方文档 ...

  8. 分享一个自己写的MVC+EF “增删改查” 无刷新分页程序

    分享一个自己写的MVC+EF “增删改查” 无刷新分页程序 一.项目之前得添加几个组件artDialog.MVCPager.kindeditor-4.0.先上几个效果图.      1.首先建立一个数 ...

  9. MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得。

    MVC已经是现代Web开发中的一个很重要的部分,下面介绍一下Spring MVC的一些使用心得. 之前的项目比较简单,多是用JSP .Servlet + JDBC 直接搞定,在项目中尝试用 Strut ...

随机推荐

  1. <Spring Cloud>入门五 hystrix

    1.服务熔断 1.1引入坐标 <dependency> <groupId>org.springframework.cloud</groupId> <artif ...

  2. qt c++对象头文件如何相互包含

    今天在写qt时,遇到了两个类相互包含的问题,类A要用到类B,类B要用到类A. 类A:a.h #ifndef A_H #define A_H #include <b.h> class A { ...

  3. go语言的碎片整理:time

    时间和日期是我们编程中经常用到的,本文主要介绍了Go语言内置的time包的基本用法. Go语言中导入包 单行导入 import "time" import "fmt&qu ...

  4. Jmeter接口测试-正则表达式提取器-提取token

    在使用Jmeter过程中会有这样的场景, A接口执行后返回json字符串, 这个json中有B接口需要的某一个参数, 那如何来实现呢? 第一步:添加正则表达式 方法非常简单, 这就是我们今天要讲的正则 ...

  5. NYOJ27水池数目,类似于FZU1008最大黑区域,简单搜索题~~~

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地图上 ...

  6. [luoguP3606] [USACO17JAN]Building a Tall Barn建谷仓(贪心 + 线段树)

    传送门 把线段都读进来然后排序,先按右端点为第一关键字从小到大排序,后按左端点为第二关键字从小到大排序. 注意不能先按左端点后按右端点排序,否则会出现大包小的情况,如下: —————— ———  — ...

  7. 多边形之战(bzoj 2927)

    Description 多边形之战是一个双人游戏.游戏在一个有n个顶点的凸多边形上进行,这个凸多边形的n-3条对角线将多边形分成n-2个三角形,这n-3条对角线在多边形的顶点相交.三角形中的一个被染成 ...

  8. [NOIP2000] 提高组 洛谷P1022 计算器的改良

    题目背景 NCL是一家专门从事计算器改良与升级的实验室,最近该实验室收到了某公司所委托的一个任务:需要在该公司某型号的计算器上加上解一元一次方程的功能.实验室将这个任务交给了一个刚进入的新手ZL先生. ...

  9. hdu 5188 zhx and contest [ 排序 + 背包 ]

    传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  10. Free Web Application Firewall相关资料

    http://www.freewaf.org/solution/#1 http://baike.soso.com/v60659982.htm