设计模式——观察者模式

名称Observer
结构 Observer.gif
意图定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。
适用性
  • 当一个抽象模型有两个方面, 其中一个方面依赖于另一方面。将这二者封装在独立的对象中以使它们可以各自独立地改变和复用。
  • 当对一个对象的改变需要同时改变其它对象, 而不知道具体有多少对象有待改变。
  • 当一个对象必须通知其它对象,而它又不能假定其它对象是谁。换言之, 你不希望这些对象是紧密耦合的。
Code Example
  1 None.gif //  Observer
  2 None.gif
  3 None.gif //  Intent: "Define a one-to-many dependency between objects so that when one 
  4 None.gif //  object changes state, all its dependents are notified and 
  5 None.gif //  updated automatically". 
  6 None.gif
  7 None.gif //  For further information, read "Design Patterns", p293, Gamma et al.,
  8 None.gif //  Addison-Wesley, ISBN:0-201-63361-2
  9 None.gif
 10 ExpandedBlockStart.gifContractedBlock.gif /**/ /* Notes:
 11InBlock.gif * Often used for Document-View architectures, where a document stores 
 12InBlock.gif * data and one or more views renders the data. 
 13ExpandedBlockEnd.gif */

 14 None.gif 
 15 None.gif namespace  Observer_DesignPattern
 16 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 17InBlock.gif    using System;
 18InBlock.gif    using System.Collections;
 19InBlock.gif
 20InBlock.gif    class Subject
 21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 22InBlock.gif        private ArrayList list = new ArrayList();
 23InBlock.gif
 24InBlock.gif        private string strImportantSubjectData = "Initial";
 25InBlock.gif        
 26InBlock.gif        public string ImportantSubjectData 
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 28InBlock.gif            get 
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 30InBlock.gif                return strImportantSubjectData;
 31ExpandedSubBlockEnd.gif            }

 32InBlock.gif            set 
 33ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 34InBlock.gif                strImportantSubjectData = value;
 35ExpandedSubBlockEnd.gif            }

 36ExpandedSubBlockEnd.gif        }

 37InBlock.gif
 38InBlock.gif        public void Attach(Observer o)
 39ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 40InBlock.gif            list.Add(o);
 41InBlock.gif            o.ObservedSubject = this;
 42ExpandedSubBlockEnd.gif        }

 43InBlock.gif
 44InBlock.gif        public void Detach(Observer o)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            
 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif
 49InBlock.gif        public void Notify()
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 51InBlock.gif            foreach (Observer o in list)        
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 53InBlock.gif                o.Update();
 54ExpandedSubBlockEnd.gif            }
            
 55ExpandedSubBlockEnd.gif        }

 56ExpandedSubBlockEnd.gif    }

 57InBlock.gif
 58InBlock.gif    class ConcreteSubject : Subject
 59ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 60InBlock.gif        public void GetState()
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62InBlock.gif            
 63ExpandedSubBlockEnd.gif        }

 64InBlock.gif
 65InBlock.gif        public void SetState()
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 67InBlock.gif            
 68ExpandedSubBlockEnd.gif        }
    
 69ExpandedSubBlockEnd.gif    }

 70InBlock.gif
 71InBlock.gif    abstract class Observer 
 72ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 73InBlock.gif        protected Subject s;
 74InBlock.gif        public Subject ObservedSubject 
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 76InBlock.gif            get 
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 78InBlock.gif                return s;        
 79ExpandedSubBlockEnd.gif            }

 80InBlock.gif            set 
 81ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 82InBlock.gif                s = value;
 83ExpandedSubBlockEnd.gif            }

 84ExpandedSubBlockEnd.gif        }
    
 85InBlock.gif        abstract public void Update();
 86ExpandedSubBlockEnd.gif    }

 87InBlock.gif
 88InBlock.gif    class ConcreteObserver : Observer 
 89ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 90InBlock.gif        private string observerName;
 91InBlock.gif        
 92InBlock.gif        public ConcreteObserver(string name)
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 94InBlock.gif            observerName = name;
 95ExpandedSubBlockEnd.gif        }

 96InBlock.gif
 97InBlock.gif        override public void Update()
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 99InBlock.gif            Console.WriteLine("In Observer {0}: data from subject = {1}"
100InBlock.gif                observerName, s.ImportantSubjectData);
101ExpandedSubBlockEnd.gif        }
    
102ExpandedSubBlockEnd.gif    }

103InBlock.gif
104ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
105InBlock.gif    ///    Summary description for Client.
106ExpandedSubBlockEnd.gif    /// </summary>

107InBlock.gif    public class Client
108ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{     
109InBlock.gif        public static int Main(string[] args)
110ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{          
111InBlock.gif            // Set up everything
112InBlock.gif            ConcreteSubject s = new ConcreteSubject();
113InBlock.gif            ConcreteObserver o1 = new ConcreteObserver("first observer");
114InBlock.gif            ConcreteObserver o2 = new ConcreteObserver("second observer");
115InBlock.gif
116InBlock.gif            s.Attach(o1);
117InBlock.gif            s.Attach(o2);
118InBlock.gif
119InBlock.gif            // make changes to subject
120InBlock.gif            s. ImportantSubjectData = "This is important subject data";
121InBlock.gif
122InBlock.gif            // Notify all observers
123InBlock.gif            s.Notify();            
124InBlock.gif            return 0;
125ExpandedSubBlockEnd.gif        }

126ExpandedSubBlockEnd.gif    }

127ExpandedBlockEnd.gif}

128 None.gif

转载于:https://www.cnblogs.com/DarkAngel/archive/2005/08/16/215736.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值