/// <summary> /// 注册 /// </summary> /// <param name="o"></param> public void Attach(CObject o) { if (!ObjectList.Contains(o)) { ObjectList.Add(o); } }
/// <summary> /// 删除 /// </summary> /// <param name="o"></param> public void Dettach(CObject o) { if (ObjectList.Contains(o)) { ObjectList.Remove(o); } }
static void Main(string[] args) { ISubject sbj = new CSubject(); CObject obj1 = new CObject(); CObject obj2 = new CObject(); sbj.Attach(obj2); sbj.Attach(obj1); while (true) { char tmpChar = Console.ReadKey().KeyChar; if (tmpChar.Equals('Q')) { break; } if (tmpChar.Equals('R')) { sbj.Dettach(obj1); } sbj.BroadObject = tmpChar; Console.WriteLine(); // 通知订阅者 sbj.Notify(); } }