namespace MVPPattern { interface IView { void Button_Click(Object sender, EventArgs e); void Update(int value); void CompositeElement(); } }
视图接口
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MVPPattern { partial class Form1 : Form,IView { private int _Value; private TextBox _TextBox; private TextBox _TextBox2; private Button _ReadButton; private Button _WriteButton; private Panel _Container; private IController _Controller; public Form1() { InitializeComponent(); } public Form1(IController controller, IModel model):this() { CompositeElement(); _Controller = controller; model.AddObserver(this); model.NotifyObserver(); } #region IView 成员 public void Button_Click(Object sender, EventArgs e) { Button btn = (Button)sender; switch (btn.Text) { case "获取值": _Controller.ReadValue(); break; case "设置值": _Controller.WriteValue(int.Parse(_TextBox2.Text)); break; } } public void Update(int value) { _Value = value; _TextBox.Text = _Value.ToString(); } public void CompositeElement() { _TextBox = new TextBox(); _TextBox2 = new TextBox(); _ReadButton = new Button(); _WriteButton = new Button(); _Container = new Panel(); this.Controls.Add(_Container); _Container.Controls.Add(_TextBox); _Container.Controls.Add(_TextBox2); _Container.Controls.Add(_ReadButton); _Container.Controls.Add(_WriteButton); _TextBox.ReadOnly = true; _ReadButton.Text = "获取值"; _ReadButton.Click+=new EventHandler(Button_Click); _WriteButton.Text = "设置值"; _WriteButton.Click+=new EventHandler(Button_Click); _Container.Width = 300; _Container.Height = 200; _TextBox.Location = new Point(0, 0); _TextBox2.Location = new Point(0, 25); _ReadButton.Location = new Point(0,50); _WriteButton.Location = new Point(0,75); } #endregion } }
视图接口的实现A
namespace MVPPattern { interface IController { void ReadValue(); void WriteValue(int value); } }
控制器接口
using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Forms; namespace MVPPattern { class Controller:IController { private IModel _Model; private Form _View; public Controller(IModel model) { _Model = model; _View = new Form1(this, _Model); Application.Run(_View); _View.Focus(); } #region IController 成员 public void ReadValue() { _Model.ReadValue(); } public void WriteValue(int value) { _Model.WriteValue(value); } #endregion } }
控制器接口的实现A
namespace MVPPattern { interface IModel { void ReadValue(); void WriteValue(int value); void AddObserver(IView view); void NotifyObserver(); } }
模型接口
using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace MVPPattern { class Model:IModel { private int _Value; private ArrayList _ObserverList; public Model(int value) { _Value = value; _ObserverList = new ArrayList(); } #region IModel 成员 public void ReadValue() { NotifyObserver(); } public void WriteValue(int value) { _Value = value; //NotifyObserver(); } public void AddObserver(IView view) { _ObserverList.Add(view); } public void NotifyObserver() { foreach (IView v in _ObserverList) v.Update(_Value); } #endregion } }
模型接口的实现A
using System; using System.Collections.Generic; using System.Text; namespace MVPPattern { class Program { static void Main(string[] args) { Model model = new Model(10); Controller controller = new Controller(model); } } }
调用者