观察者模式实践
观察者模式定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。
【原创】欢迎转贴
Observer.cs Code
Form2.cs 代码
【原创】欢迎转贴
Observer.cs Code
// 可被观察类的基类
public abstract class Observable
{
protected ArrayList observerList = new ArrayList();
//添加观察者
public virtual void AddObserver(IObserver observer)
{
observerList.Add(observer);
}
//删除观察者
public virtual void RemoveObserver(IObserver observer)
{
observerList.Remove(observer);
}
//清除观察者
public virtual void ClearObservers()
{
observerList.Clear();
}
/// <summary>
/// 通知观察者
/// </summary>
/// <param name="argName"></param>
/// <param name="argValue"></param>
public virtual void NotifyObservers(string argName,object argValue)
{
foreach(object o in observerList)
{
//通知观察者进行更新
((IObserver)o).Update(argName,argValue);
}
}
}
//观察者的接口
public interface IObserver
{
void Update(string argName,object argValue);
}
//可被观察类
public class Data:Observable
{
public Data()
{
}
public string Name
{
set
{
this.NotifyObservers("name",value);
}
}
public int Age
{
set
{
this.NotifyObservers("age",value);
}
}
}
//一种观察者,只关心名字
public class NameObserver:IObserver
{
public string describe ;
public NameObserver()
{
}
IObserver 成员
}
//另一种观察者,只关心年龄
public class AgeObserver:IObserver
{
public string describe ;
public AgeObserver()
{
}
IObserver 成员
}
public abstract class Observable
{
protected ArrayList observerList = new ArrayList();
//添加观察者
public virtual void AddObserver(IObserver observer)
{
observerList.Add(observer);
}
//删除观察者
public virtual void RemoveObserver(IObserver observer)
{
observerList.Remove(observer);
}
//清除观察者
public virtual void ClearObservers()
{
observerList.Clear();
}
/// <summary>
/// 通知观察者
/// </summary>
/// <param name="argName"></param>
/// <param name="argValue"></param>
public virtual void NotifyObservers(string argName,object argValue)
{
foreach(object o in observerList)
{
//通知观察者进行更新
((IObserver)o).Update(argName,argValue);
}
}
}
//观察者的接口
public interface IObserver
{
void Update(string argName,object argValue);
}
//可被观察类
public class Data:Observable
{
public Data()
{
}
public string Name
{
set
{
this.NotifyObservers("name",value);
}
}
public int Age
{
set
{
this.NotifyObservers("age",value);
}
}
}
//一种观察者,只关心名字
public class NameObserver:IObserver
{
public string describe ;
public NameObserver()
{
}
IObserver 成员
}
//另一种观察者,只关心年龄
public class AgeObserver:IObserver
{
public string describe ;
public AgeObserver()
{
}
IObserver 成员
}
Form2.cs 代码
private Data data = new Data();
private NameObserver ob = new NameObserver();
private AgeObserver ob2 = new AgeObserver();
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
try
{
data.Name = textBox1.Text;
ShowInfo();
}
catch
{
}
}
private void textBox4_TextChanged(object sender, System.EventArgs e)
{
try
{
data.Age = Int32.Parse(textBox4.Text);
ShowInfo();
}
catch
{
}
}
private void ShowInfo()
{
textBox2.Text = ob.describe ;
textBox3.Text = ob2.describe ;
}
private void Form2_Load(object sender, System.EventArgs e)
{
data.AddObserver(ob);
data.AddObserver(ob2);
}
private NameObserver ob = new NameObserver();
private AgeObserver ob2 = new AgeObserver();
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
try
{
data.Name = textBox1.Text;
ShowInfo();
}
catch
{
}
}
private void textBox4_TextChanged(object sender, System.EventArgs e)
{
try
{
data.Age = Int32.Parse(textBox4.Text);
ShowInfo();
}
catch
{
}
}
private void ShowInfo()
{
textBox2.Text = ob.describe ;
textBox3.Text = ob2.describe ;
}
private void Form2_Load(object sender, System.EventArgs e)
{
data.AddObserver(ob);
data.AddObserver(ob2);
}