2010年11月7日 星期日

[Patterns] MVC pattern

簡介

最近在研究怎麼自己弄簡單的MVC,所以研究了一下 iphone 的 UIView 與 UIViewController 之間的運作模式


Sample Code
interface ITouch
{
void touch();
}

class Responser : ITouch
{
ITouch delegate;

public ITouch Delegate
{
get { return delegate; }
set { delegate = value; }
}
protected void touch()
{
if(delegate != null)
delegate->touch();
}
}

class UIView : Responser
{
}

class UITestView : UIView
{
Button btnTest;
Label lblTest;
public UITestView
{
btnTest = new Button();
lblTest = new Label();
btnTest.Delegate = this.Delegate;
this.Add(btnTest);
this.Add(lblTest);
}
public void ShowText()
{
lblTest.Text = btnTest.Text;
}
}

class UIViewController : Response
{
}

class UITestViewController : UIViewController
{
UITestView view;
public UIViewController()
{
view = new UITestView();
view.Delegate = this;
}

void override touch()
{
view.ShowText();
}
}

UML

沒有留言:

張貼留言

嘎嘎嘎