模型视图控制器设计模式代码示例 [英] Model View Controller Design pattern Code Example

查看:82
本文介绍了模型视图控制器设计模式代码示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究模型 - 视图 - 控制器设计模式,我理解背后的概念背后的概念,但我想了解一下实际如何实践。

维基百科提到Wt - Web工具包,CppCMS和一些其他使用该模式的标准实现,但我并不熟悉这些,我只是希望,
将非常感谢如果有人可以提供一些示例代码(希望是C ++),其中实现模式,解释实践模式理论。

解决方案

这是一个快速的例子(没有尝试编译,让我知道是否有错误):

  class Button; //预写的GUI元素

class GraphGUI {
public:
GraphGUI(){
_button = new Button(Click Me);
_model = new GraphData();
_controller = new GraphController(_model,_button);
}
〜GraphGUI(){
delete _button;
delete _model;
delete _controller;
}

drawGraph(){
//使用模型的数据来绘制图表
}
...

private:
按钮* _button;
GraphData * _model;
GraphController * _controller;
};

class GraphData {
public:
GraphData(){
_number = 10;
}
void increaseNumber(){
_number + = 10;
}
const int getNumber(){return _number; }
private:
int _number;
};

class GraphController {
public:
GraphController(GraphData * model,Button * button){
__model = model;
__button =按钮;
__button-> setClickHandler(this,& onButtonClicked);
}

void onButtonClicked(){
__model-> increaseNumber();
}

private:
//不处理内存
GraphData * __model;
按钮* __button;
};

忽略Button的实现,基本上这个程序将使用GraphGUI来显示一个图形,当一个按钮被按下。假设这是一个条形图,它会变得更高。



由于模型独立于视图(按钮),控制器处理两者之间的通信,这是遵循MVC模式。



当按钮被点击时,控制器通过onButtonClicked函数来修改模型,当Button类被点击时,它会知道调用。 / p>

这个优点是因为模型和视图是完全独立的,每个的实现都会发生巨大变化,而不会影响到另一个,控制器可能只需要做一些改变如果在这种情况下的模型根据某些数据库数据计算了一些结果,那么单击该按钮可能会导致这种情况发生,但是按钮的实现将不必改变。或者,而不是告诉控制器发生点击时,也许它可以告诉控制器何时按钮被鼠标悬停。无论触发更改是什么,都会对模型应用相同的更改。


I was studying the Model-View-Controller design pattern and i understand the concept behind the pattern theorotically, but I wanted to get a peek at how one would actually put it to practice.
Wikipedia mentions Wt - Web toolkit, CppCMS and some other standard implementations which use the pattern however I have not been familiar with these, and I was just hoping and will be really grateful If anyone can provide some sample code(hopefully C++) which implements the pattern and explains the theory of the pattern being put to practice.

解决方案

Here's a quick example I made (didn't try compiling it, let me know if there's errors):

class Button; // Prewritten GUI element

class GraphGUI {
public:
    GraphGUI() {
        _button = new Button("Click Me");
        _model = new GraphData();
        _controller = new GraphController(_model, _button);
    }
    ~GraphGUI() {
        delete _button;
        delete _model;
        delete _controller;
    }

    drawGraph() {
        // Use model's data to draw the graph somehow
    }
    ...

private:
    Button*              _button;
    GraphData*           _model;
    GraphController*     _controller;
};

class GraphData {
public:
    GraphData() {
        _number = 10; 
    }
    void increaseNumber() {
        _number += 10;
    }
    const int getNumber() { return _number; }
private:
    int _number;
};

class GraphController {
public:
    GraphController(GraphData* model, Button* button) {
        __model = model;
        __button = button;
        __button->setClickHandler(this, &onButtonClicked);
    }

    void onButtonClicked() {
        __model->increaseNumber();
    }

private:
    // Don't handle memory
    GraphData*    __model;
    Button*       __button; 
};

Ignoring the implementation of Button, basically this program will use GraphGUI to display a graph that will change when a button is pressed. Let's say it's a bar graph and it will get taller.

Since the model is independent of the view (the button), and the controller handles the communication between the two, this follows the MVC pattern.

When the button is clicked, the controller modifies the model via the onButtonClicked function, which the Button class knows to call when it is clicked.

The beauty of this is since the model and view are completely independent, the implementation of each can drastically change and it won't affect the other, the controller might simply have to make a few changes. If the model in this case calculated some result based off some database data, then clicking the button could cause this to happen, but the button implementation wouldn't have to change. Or, instead of telling the controller when a click occurs, maybe it can tell the controller when the button is moused-over. The same changes are applied to model, regardless of what triggered the changes.

这篇关于模型视图控制器设计模式代码示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆