蛔虫是什么动物| 总蛋白偏高有什么危害| 得艾滋病有什么症状| 半月板变性是什么意思| 什么如既往| 限期使用日期是什么意思| 黄色裤子搭配什么颜色上衣| 你把我当什么| 一饿就胃疼是什么原因| 浑身疼吃什么药管用| 庸人自扰什么意思| 处女座前面是什么星座| 812是什么意思| 梦见打蛇是什么预兆| 备孕喝苏打水什么作用| 眼睛一直跳是什么原因| 铁达时手表什么档次| 蒲公英可以和什么一起泡水喝| 什么叫便溏| 办狗证需要什么资料| r0lex是什么牌子手表| 上挂是什么意思| 电是什么时候发明的| 梦到黄鳝是什么意思| 气血不足吃什么| 猪和什么生肖最配| 红楼梦是一部什么小说| 腊肉炒什么菜最好吃| 头孢加酒有什么反应| 脚后跟痛什么原因| 冷萃是什么意思| 肚子疼吃什么药最有效| 柴鸡蛋是什么| 什么植物和动物像鸡| 肛门潮湿是什么情况| 果五行属什么| 教师节送什么礼物给老师| 蝴蝶是什么意思| 上眼药什么意思| 风信子的花语是什么| 什么是绿茶女| 参谋长是什么军衔| 哲字五行属什么| 双侧卵巢显示不清是什么意思| 寸头是什么意思| 十滴水是什么| 喝酒对身体有什么危害| 鼓包是什么意思| 吴亦凡什么星座| 心理健康是什么| 经常爱放屁是什么原因| 脉压差小是什么原因| cpi下降意味着什么| 第一次世界大战是什么时候| 感冒鼻塞吃什么药| 漂发是什么意思| pet一ct是一种什么检查| 胸部什么时候停止发育| 预防心肌梗塞吃什么药最好| 肌酐高可以吃什么水果| 瞳孔放大意味着什么| 女人做梦哭醒预示什么| 经血发黑是什么原因| 乳头痒是怎么回事是什么原因| 什么人容易得小脑萎缩| 蓝莓泡酒有什么功效| 死缓是什么意思| 早上起来头晕是什么原因| 脸色发红什么原因| 香草是什么意思| 农历7月是什么星座| 半月板后角变性什么意思| 什么牌子的氨基酸洗面奶好| 甲功不正常有什么表现| 什么含胶原蛋白最多| 县政府党组成员什么级别| 舌头红是什么原因| 得了咽炎有什么症状| 胰岛素ins是什么意思| 胃角在什么位置图片| 认知障碍是什么病| 榴莲什么季节成熟| 磨牙是缺什么| 右手心痒是什么预兆| 什么是气质| 感情是什么| 脉弱是什么意思| 手心发热吃什么药最好| 牙龈出血吃什么药| 检查胰腺挂什么科| xrd是什么| 诚字属于五行属什么| 高密度脂蛋白胆固醇偏高什么意思| 吃什么可以补阳气| 甘油三酯高吃什么能降下来| 睡眠不好是什么原因引起的| 内膜欠均匀是什么意思| 银行降息意味着什么| 不好意思是什么意思| 今天开什么奖| 野鸡吃什么| 电器着火用什么灭火器| 梦见撒尿是什么意思| 片仔癀有什么功效| 蚕长什么样| 胃窦糜烂是什么意思严重吗| 肾结石能吃什么水果| 麻辣拌里面都有什么菜| 异地办理护照需要什么材料| 苯对人体有什么危害| 什么死法不痛苦| 牙龈出血什么原因| 肝郁脾虚吃什么药| 什么情况下安装心脏起搏器| 法克是什么意思| 点了斑不能吃什么| 孕妇睡觉流口水是什么原因| 吃什么可以解酒最快简单| 啃老是什么意思| 男性下焦湿热吃什么药| 戒腊什么意思| 生化妊娠是什么原因导致的| 看脱发应该挂什么科| 35岁月经量少是什么原因| 单核细胞百分比偏高说明什么| 盐酸哌替啶是什么药| 局级是什么级别| 秦始皇的原名叫什么| 日加匀念什么| 外贸原单是什么意思| 1978年是什么命| 颈椎病吃什么药好| 吃维生素b2有什么好处| 珍惜当下是什么意思| 拉肚子吃什么最好| 招蚊子咬是什么血型| 菠萝蜜是什么季节的水果| 戴玉对身体有什么好处| 刘姥姥和贾府什么关系| 王为念和王芳什么关系| 什么情况会导致月经推迟不来| 脚麻是什么原因| john是什么意思| 性格内敛是什么意思| 为什么小孩子有白头发| 晚上7点到9点是什么时辰| 沙弗莱是什么宝石| 青岛有什么玩的| 什么东西辟邪| 早上空腹喝淡盐水有什么好处| 持之以恒的恒是什么意思| 什么叫轻食| 农历六月十八是什么日子| 为什么会胃疼| click什么意思| 端午节为什么吃粽子| 梦见好多鱼是什么意思| 庚申五行属什么| 孕早期生气对胎儿有什么影响| 相位是什么意思| 想长胖喝什么奶粉好| 韭菜什么时候种| 为什么健身| 老年人口苦是什么原因| 喝什么最容易减肥| 甜瓜不能和什么一起吃| 2005年属什么| 强盗是什么意思| 2016年是什么生肖| 什么样的孙悟空| 4月5号是什么星座| 甲壳虫吃什么食物| 兔子能吃什么水果| 什么是达人| 一个月一个非念什么| 壮字五行属什么| 透析病人吃什么水果好| 拔牙后吃什么食物最好| 生的反义词是什么| 身体不出汗是什么原因| 寒潮是什么| 气血两虚吃什么中成药| 蟑螂为什么会飞| 码子是什么意思| 缺钾会有什么症状| 冬至吃什么馅的饺子| 咖啡有什么营养| 空前绝后是什么生肖| 返祖现象什么意思| 摸摸唱是什么| 善良对什么| 促狭一笑是什么意思| 泛性恋是什么意思| 办什么厂比较好| 总是爱出汗是什么原因| 什么叫道德| 肌肉萎缩是什么症状| 不小心怀孕了吃什么药可以流掉| 珍珠龟吃什么| 儿童过敏性鼻炎吃什么药好| 餐后血糖高是什么原因| 裸车是什么意思| 吉可以加什么偏旁| 肾结石挂什么科| 人怕出名猪怕壮是什么生肖| 爱居兔女装是什么档次| 半夜胎动频繁是什么原因| 什么人什么目| 氧分压低是什么原因| 转氨酶高挂什么科| 儿童水痘吃什么药| dsa检查是什么| 喉咙痛吃什么药好得最快| 退役和退伍有什么区别| 铁锈是什么| 人参片泡水喝有什么功效和作用| 拍身份证照片穿什么衣服| 毕业穿的衣服叫什么| flag是什么意思| ACG是什么牌子| 胰岛素是什么意思| 冬天喝什么汤| 脑供血不足吃什么药效果最好| 季夏是什么意思| 尿微量白蛋白是什么意思| 成手是什么意思| 湿疹不能吃什么| 小别胜新婚什么意思| 下巴下面长痘痘是什么原因| e-mail什么意思| 自愿离婚要带什么证件| 儿童发烧挂什么科| 进去是什么感觉| 肌肉拉伤有什么症状| 脚肿是什么病的前兆| 肾炎吃什么食物好| 手麻挂什么科室| 2月出生是什么星座| 妹控是什么意思| 男生为什么要做包皮手术| 同人文什么意思| 孕妇适合喝什么牛奶| 什么是前列腺钙化| 夏天吃什么水果好| 女性什么时间是排卵期| 每年什么时候征兵| 打呼噜是什么原因| po医学上是什么意思| 跳空缺口是什么意思| 积食内热吃什么药| 字母圈是什么意思| 师长是什么级别| 什么是荷尔蒙| 家里进黄鼠狼是什么预兆| 电解质是什么| poppy什么意思| 胆固醇高不能吃什么水果| 阉割什么意思| 恶心干呕吃什么药| 小腿酸胀是什么原因| 河南的特产是什么| 917是什么星座| 农历3月3是什么节日| 绅士是什么意思| 百度
65.9K
CodeProject is changing. Read more.
Home

卖房送女留学却嫁老外 父母崩溃:我们老了谁照顾

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon
百度 要围绕统筹推进“五位一体”总体布局和协调推进“四个全面”战略布局中的重大问题,事关人民群众切身利益的热点难点问题,搭建协商平台,健全协商方式,不断提高政治协商的实效性,充分发挥政治协商对我们党提高领导水平和长期执政能力的促进作用。

3.82/5 (33 votes)

Apr 8, 2008

CPOL

3 min read

viewsIcon

944993

downloadIcon

20030

Simple example of MVC (Model View Controller) design pattern for abstraction

Introduction

Model-view-controller (MVC) is a pattern used to isolate business logic from the user interface. Using MVC, the Model represents the information (the data) of the application and the business rules used to manipulate the data, the View corresponds to elements of the user interface such as text, checkbox items, and so forth, and the Controller manages details involving the communication between the model and view. The controller handles user actions such as keystrokes and mouse movements and pipes them into the model or view as required.

Figure4.gif

Background

Using the Code

Note: I strongly recommend you download the code to view it, it will be much easier.

Here I will show an example of our good old friend calculator in a MVC architecture. A brief overview; the Form will house the view and events will be passed to the controller who will then call methods on our model such as ADD/Subtract/NumberPress. The model takes care of all the work and it holds the current state of the calculator. The tough thing about MVC is where to slice it apart can be confusing. The end goal is a pluggable UI and perhaps multiple controllers attached to the same model. So one way to test if you did it right is to quickly write another UI and plug it in.

A typical MVC patterns instantiation looks something like the following. A few important things to notice; the controller takes an interface to the view and model. It is important to know that the view will typically interact with the controller if it needs notification of events which are fired via the view (such as a button click). In this case, I have the controllers constructor pass a reference to itself to the view class.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
// Note: The view should not send to the model but it is often useful
// for the view to receive update event information from the model. 
// However you should not update the model from the view.
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        frmCalcView view = new frmCalcView();
        CalculatorModel model = new CalculatorModel();
        CalcController controller = new CalcController(model, view);
        Application.Run(view);
    }
}
/// <summary>
/// The controller process the user requests.
/// Based on the user request, the Controller calls methods in the View and
/// Model to accomplish the requested action. 
/// </summary>
class CalcController : IController
{
    ICalcModel model;
    ICalcView view;

    public CalcController( ICalcModel model, ICalcView view)
    {
        this.model = model;
        this.view = view;
        this.view.AddListener(this); // Pass controller to view here.
    }

    public void OnClick( int number )
    {
        view.Total = model.SetInput(number).ToString();
    }

    public void OnAdd()
    {
        model.ChangeToAddState(); 
    }
}

Notice that the view does not interact with the model, it simply receives update requests from the controller. The controller will access the view through the Total property. The view also passes click events on to the controller.

This View shouldn't know about the Controller, except we need to give it notification of some events so we pass in a IController. We will invoke event handlers on the controller via IController.

/// <summary>
/// Windows Form that will host our MVC based functionality.
/// 
/// </summary>
public partial class frmCalcView : Form, ICalcView
{
    IController controller;
    public frmCalcView( )
    {
        InitializeComponent();
    }
    /// <summary>
    /// The view needs to interact with the controller to pass the click events
    /// This could be done with delegates instead.
    /// </summary>
    /// <param name="controller"></param>
    public void AddListener( IController controller )
    {
        this.controller = controller;
    }
    private void lbl_Click(object sender, EventArgs e)
    {
        // Get the text out of the label to determine the letter and pass the
        // click info to the controller to distribute.
        controller.OnClick((Int32.Parse(((Label)sender).Text)));
    }
    private void lblPlus_Click(object sender, EventArgs e)
    {
        controller.OnAdd();
    }

#region ICalcView Members
    public string Total
    {
        get
        {
            return textBox1.Text;
        }
        set
        {
            textBox1.Text = value;
        }
      }
#endregion
}

Now we will take a look at the model. Notice that it should do the "work" of the calculator and it handles the state.

    /// <summary>
    /// Calculator model, The model is independent of the user interface.
    /// It doesn't know if it's being used from a text-based, graphical, or web interface
    /// This particular model holds the state of the application and the current value.
    /// The current value is updated by SetInput
    /// </summary>
    class CalculatorModel : ICalcModel
    {
        public enum States { NoOperation, Add, Subtract };
        States state;
        int currentValue;
        public States State
        {
            set { state = value; }
        }
        public int SetInput ( int number )
        {
            if (state == States.NoOperation)
            {
                currentValue = number;
            }
             else if (state == States.Add)
            {
                currentValue = Add(currentValue , number );
            }
            return currentValue;
        }
        public void ChangeToAddState()
        {
            this.state = States.Add;
        }
        public int Add( int value1, int value2 )
        {
            return value1 + value2;
        }
        public int Subtract(int value1, int value2)
        {
            throw new System.ApplicationException(" Not implemented yet");
        }
}

Other Information

MVC is often seen in web applications, where the view is the actual HTML page, and the controller is the code that gathers dynamic data and generates the content within the HTML. Finally, the model is represented by the actual content, usually stored in a database or XML files, and the business rules that transform that content based on user actions.

Though MVC comes in different flavors, control flow generally works as follows:

  1. The user interacts with the user interface in some way (e.g. presses a button).
  2. A controller handles the input event from the user interface, often via a registered handler or callback.
  3. The controller notifies the model of the user action, possibly resulting in a change in the model's state. (e.g. controller updates user's shopping cart).
  4. A view uses the model (indirectly) to generate an appropriate user interface (e.g. the view produces a screen listing the shopping cart contents). The view gets its own data from the model. The model has no direct knowledge of the view.
  5. The user interface waits for further user interactions, which begins the cycle anew.

By decoupling models and views, MVC helps to reduce the complexity in architectural design, and to increase flexibility and reuse.

History

  • 8th April, 2008: Initial post
挛缩是什么意思 冒冷汗是什么原因 困水是什么意思 血钾低吃什么 香叶是什么树叶
子宫切除后对身体有什么影响 什么叫多巴胺 弥陀是什么意思 正在候车是什么意思 坐月子能吃什么
甲亢查什么 电荷是什么意思 长期低血糖对人体有什么危害 为什么支气管炎咳嗽长期不好 玉米是什么时候传入中国的
azul是什么颜色 熟络是什么意思 江西庐山产什么茶 万加一笔是什么字 小学生的学籍号是什么
耳鸣用什么药hcv8jop8ns2r.cn 抗核抗体谱检测查什么的hcv8jop0ns5r.cn 狮子座前面是什么星座hcv8jop3ns7r.cn 三月十号是什么星座hcv7jop7ns3r.cn 深圳少年宫有什么好玩的cl108k.com
什么叫电子版照片baiqunet.com 牙龈紫色是什么原因xinjiangjialails.com 绞肠痧是什么病hcv9jop5ns9r.cn 手指甲没有月牙是什么原因0297y7.com 什么牌子的耳机音质效果最好hcv7jop6ns8r.cn
授记是什么意思hcv9jop4ns8r.cn 腹泻吃什么水果好hcv8jop6ns1r.cn 下巴发黑是什么原因hcv9jop4ns1r.cn 缠足是什么时候开始的hcv9jop0ns7r.cn 藏青色是什么颜色xjhesheng.com
啃老是什么意思hcv8jop7ns4r.cn 心肌酶高是什么原因hcv8jop8ns5r.cn 好好好是什么语气hcv8jop8ns0r.cn hyundai是什么牌子hcv8jop8ns5r.cn 周瑜是什么样的人hcv8jop6ns6r.cn
百度