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