理解:订阅-发布。把所有需要通知的对象添加到发布者类里面。发布者更改自己状态后通知观察者
代码:
using System.Collections.Generic;
using System.Windows.Forms;
namespace DesignMode.Observer
{
//抽象发布者
public abstract class Group
{
public abstract void Add(Friend friend);
public abstract void Remove(Friend friend);
public abstract void Notify();
}
public class Weibo : Group
{
private string state;
public string State
{
get { return state; }
set { state = value; }
}
private IList<Friend> subs = new List<Friend>();
public override void Add(Friend friend)
{
subs.Add(friend);
}
public override void Remove(Friend friend)
{
subs.Remove(friend);
}
public override void Notify()
{
foreach (Friend f in subs)
{
f.Accept();
}
}
}
// 抽象订阅者
public abstract class Friend
{
public abstract void Accept();
}
public class ZhangSan : Friend
{
private string _myState;
private string _name;
private Weibo _wb;
public ZhangSan(Weibo wb, string name)
{
this._name = name;
this._wb = wb;
}
public override void Accept()
{
_myState = _wb.State;
if (_myState.Equals("发送"))
{
MessageBox.Show(_name + "收到WB信息");
}
else
{
MessageBox.Show(_name + "等待WB信息");
}
}
}
public class LiSi : Friend
{
private string _myState;
private string _name;
private Weibo _wb;
public LiSi(Weibo wb, string name)
{
this._name = name;
this._wb = wb;
}
public override void Accept()
{
_myState = _wb.State;
if (_myState.Equals("发送"))
{
MessageBox.Show(_name + "收到WB信息");
}
else
{
MessageBox.Show(_name + "等待WB信息");
}
}
}
}
using System.Windows.Forms;
namespace DesignMode.Observer
{
//抽象发布者
public abstract class Group
{
public abstract void Add(Friend friend);
public abstract void Remove(Friend friend);
public abstract void Notify();
}
public class Weibo : Group
{
private string state;
public string State
{
get { return state; }
set { state = value; }
}
private IList<Friend> subs = new List<Friend>();
public override void Add(Friend friend)
{
subs.Add(friend);
}
public override void Remove(Friend friend)
{
subs.Remove(friend);
}
public override void Notify()
{
foreach (Friend f in subs)
{
f.Accept();
}
}
}
// 抽象订阅者
public abstract class Friend
{
public abstract void Accept();
}
public class ZhangSan : Friend
{
private string _myState;
private string _name;
private Weibo _wb;
public ZhangSan(Weibo wb, string name)
{
this._name = name;
this._wb = wb;
}
public override void Accept()
{
_myState = _wb.State;
if (_myState.Equals("发送"))
{
MessageBox.Show(_name + "收到WB信息");
}
else
{
MessageBox.Show(_name + "等待WB信息");
}
}
}
public class LiSi : Friend
{
private string _myState;
private string _name;
private Weibo _wb;
public LiSi(Weibo wb, string name)
{
this._name = name;
this._wb = wb;
}
public override void Accept()
{
_myState = _wb.State;
if (_myState.Equals("发送"))
{
MessageBox.Show(_name + "收到WB信息");
}
else
{
MessageBox.Show(_name + "等待WB信息");
}
}
}
}
客户端代码:
private void btn_Observer_Click(object sender, EventArgs e)
{
Weibo wb = new Weibo();
wb.Add(new ZhangSan(wb, "张三"));
wb.Add(new LiSi(wb, "李四"));
wb.State = "未发送";
wb.Notify();
wb.State = "发送";
wb.Notify();}
{
Weibo wb = new Weibo();
wb.Add(new ZhangSan(wb, "张三"));
wb.Add(new LiSi(wb, "李四"));
wb.State = "未发送";
wb.Notify();
wb.State = "发送";
wb.Notify();}