观察者模式关注一个一对多的依赖关系:当一个对象发生变化时,多个与其有关联的对象都要被通知到,并更新自己。
     有一个题目:猫叫,主人醒,老鼠跑。通常我们用事件委托方式来解决这个问题。但想来,这个场景很有点观察者模式的味道。

 

     观察者模式通常包括四个角色:
     1 抽象通知者

     2 具体通知者

     3 抽象观察者

     4 具体观察者

 

     一个简单的例子:秘书看到老板回来,通知“不务正业”的同志。

 

我读设计模式之观察者模式(Observer Pattern)using System;
我读设计模式之观察者模式(Observer Pattern)
using System.Collections.Generic;
我读设计模式之观察者模式(Observer Pattern)
using System.Text;
我读设计模式之观察者模式(Observer Pattern)
我读设计模式之观察者模式(Observer Pattern)
namespace Demo4

 

      当然,用观察者模式实现的通知功能,用代理也可以轻松实现。下面的代码就用代理机制实现相同的功能。

 

 

 Demo4
{
    class Program
    {
        
static void Main(string[] args)
        {
            secretary sec 
= new ConcreteSecretary("Boss is coming");

            NBAObserver nba
=new NBAObserver("ivan", sec);
            NewsObserver news
=new NewsObserver("ashley", sec);

            
//增加委托对象
            sec.OnNotify+=new secretary.NotifyEventArgs(nba.update);
            sec.OnNotify
+=new secretary.NotifyEventArgs(news.update);

            sec.notify();

            Console.Read();
        }
    }

    
public abstract class secretary //抽象通知者
    {
        
private string _subject;

        
public string Subject
        {
            
get { return _subject; }
            
set { _subject = value; }
        }
        
public secretary(string subject)
        {
            
this._subject = subject;
        }

        
public delegate void NotifyEventArgs(object obj, EventArgs e);

        
public event NotifyEventArgs OnNotify;

        
//通知
        public void notify()
        {
            
//触发事件
            OnNotify(null, EventArgs.Empty);
        }
    }

    
//具体通知者
    public class ConcreteSecretary : secretary
    {
        
public ConcreteSecretary(string subject)
            : 
base(subject)
        { }


    }

    
public interface IObserver  //抽象观察者
    {
        
void update(object o,EventArgs e);
    }
    
public class NBAObserver : IObserver  //具体观察者(NBA Observer)
    {
        
private string _name;

        
public string Name
        {
            
get { return _name; }
            
set { _name = value; }
        }

        
private secretary _secretary;

        
public secretary Secretary
        {
            
get { return _secretary; }
            
set { _secretary = value; }
        }

        
public NBAObserver(string name, secretary secr)
        {
            
this._name = name;
            
this._secretary = secr;
        }

        
public void update(object o,EventArgs e)
        {
            Console.WriteLine(_secretary.Subject 
+ "--" + _name + " please close games and began to work");
        }
    }

    
//扩展具体的通知者,只需要新增具体类即可
    public class NewsObserver : IObserver   //具体观察者(News Observer)
    {
        
private string _name;

        
public string Name
        {
            
get { return _name; }
            
set { _name = value; }
        }

        
private secretary _secretary;

        
public secretary Secretary
        {
            
get { return _secretary; }
            
set { _secretary = value; }
        }

        
public NewsObserver(string name, secretary secr)
        {
            
this._name = name;
            
this._secretary = secr;
        }


        
public void update(object o, EventArgs e)
        {
            Console.WriteLine(_secretary.Subject 
+ "--" + _name + " please shut down the news");
        }
    }

}

 

     Observer模式的优点是实现了表示层和数据逻辑层的分离,并定义了稳定的更新消息传递机制,类别清晰,并抽象了更新接口,使得可以有各种各样不同的表示层(观察者)。

 

 

学习参考:

http://www.cnblogs.com/Terrylee/archive/2006/10/23/537778.html

http://www.cnblogs.com/zhenyulu/articles/73723.html

相关文章:

  • 2021-05-20
  • 2022-02-15
  • 2021-05-12
  • 2021-10-02
  • 2021-09-11
  • 2021-11-23
猜你喜欢
  • 2021-04-11
  • 2021-04-13
  • 2021-10-16
  • 2021-09-13
  • 2021-11-28
相关资源
相似解决方案