Exemple #1
0
        static void Main(string[] args)
        {
            ConcreteDataObject obj  = new ConcreteDataObject();
            DataModel          data = new DataModel {
                X = 1, Y = 23
            };

            obj.SetDataState(data);
            //具体观察者,指向具体目标
            Observer tab  = new TabObserver(obj);
            Observer line = new LineObserver(obj);

            //增加观察者
            obj.Add(tab);
            obj.Add(line);
            //通知
            obj.Notify();
            //修改状态
            data.X = 2;
            obj.SetDataState(data);
            obj.Notify();
            Console.WriteLine("—————委托实现Notify—————");

            ConcreteDataSubject obj1 = new ConcreteDataSubject();

            data.Y = 50;
            obj1.SetDataState(data);
            //具体观察者,指向具体目标
            TabObserver1  tab1  = new TabObserver1(obj1);
            LineObserver1 line1 = new LineObserver1(obj1);

            obj1.Update += new EventHandler(tab1.UpdateTab);
            obj1.Update += new EventHandler(line1.UpdateLine);
            obj1.Notify();
            //设置数据
            data.Y = 60;
            obj1.SetDataState(data);
            obj1.Notify();
            Console.ReadLine();
        }
 public LineObserver1(ConcreteDataSubject subject)
 {
     _subject     = subject;
     _serverState = subject.GetDataState();
 }