1/06/2010

MVP Model (Model View Presenter Design Pattern)

MVP Model (Model View Presenter Design Pattern)

View와 Business Logic을 최대한 독립적으로 만들기 위한 Design Pattern
MVC(Model View Control) Model의 진화형

Program.cs file
static class Program
{
    /// 
    /// The main entry point for the application.
    /// 
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        // IView 
        ProcessForm view = new ProcessForm();

        // IService
        Service reqText = new Service();

        // Presenter (View, Service)
        ProcessPresenter presenter = new ProcessPresenter(view, reqText);

        // Run
        Application.Run((Form)presenter.view);
    }
}

IProcessView.cs file
public interface IProcessView
{
    void NotifyUpdate(string msg);

    event EventHandler Process;
}


ProcessForm.cs file
public partial class ProcessForm : Form, IProcessView
{
    public event EventHandler Process;

    public ProcessForm()
    {
        InitializeComponent();
    }

    private void processButton_Click(object sender, EventArgs e)
    {
        EventHandler handler = this.Process;
        if (handler != null) handler(this, new EventArgs());
    }

    public void NotifyUpdate(string msg)
    {
        this.labelProcess.Text = msg;
    }
}


ProcessPresenter.cs file
public class ProcessPresenter
{
    public IProcessView view;
    public IReq req;

    public ProcessPresenter(IProcessView view, IReq req)
    {
        this.view = view;
        this.req = req;

        this.view.Process += this.OnProcess;
    }

    private void OnProcess(object source, EventArgs args)
    {
        this.view.NotifyUpdate(this.req.ToString());
    }
}

댓글 없음:

댓글 쓰기