观察者模式 C#

本文介绍了一种使用C#实现观察者模式的方法,通过定义委托和事件来模拟猫叫声触发的不同响应,演示了如何注册多个监听者并响应事件。

用C#实现观察者模式:用委托和事件

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;

namespace  ConsoleApplication3
{
    
public   class  Cat
    {
        
private   string  name;
        
public   string  Name
        {
            
get  {  return  name; }
        }
        
public   delegate   void  CatShoutEventHandler(Object sender,CatShoutEventArgs args);  // 声明委托
         public   event  CatShoutEventHandler CatShout;  // 声明事件
         public  Cat( string  name)
        {
            
this .name  =  name;
        }
        
// 事件参数
         public   class  CatShoutEventArgs : EventArgs
        {
            
private    string  sound;
            
public   string  Sound
            {
                
get  {  return  sound;}
            }
            
public  CatShoutEventArgs( string  sound)
            {
                
this .sound  =  sound;
            }
        }

        
protected   virtual   void  OnShout(CatShoutEventArgs e)
        {
            
if  (CatShout  !=   null )
            {
                CatShout(
this ,e);
            }
        }

        
public   void  shout()
        {
            
if  ( this .Name  ==   " jack " )
            {
                CatShoutEventArgs e 
=   new  CatShoutEventArgs( " large sound " );
                OnShout(e);
            }
        }

    }
    
public   class  Mouse
    {
        
private   string  name;
        
public   string  Name
        {
            
get  {  return  name;}
        }
        
public  Mouse(){
        }
        
public  Mouse( string  name) {
            
this .name  =  name;
        }
        
public   static   void  ShowMsg(Object sender, Cat.CatShoutEventArgs e)
        {   
// 静态方法
            Cat cat  =  (Cat)sender;
            Console.WriteLine(
" 有只猫,它的名字是:{0} 没有名字 快跑:  " , cat.Name);
            Console.WriteLine(
" 它的声音: " + e.Sound);
            Console.WriteLine();
        }

        
public    void  ShowMsgWithName(Object sender, Cat.CatShoutEventArgs e)
        {  
       Cat cat  =  (Cat)sender;
            Console.WriteLine(
" 有只猫,它的名字是:{0} 小老鼠{1} 快跑:  " , cat.Name, this .Name);
            Console.WriteLine(
" 它的声音: "   +  e.Sound);
            Console.WriteLine();
        }

    }
    
class  Program
    {
        
static   void  Main()
        {
            Cat cat
=   new  Cat( " jack " );
            Mouse mouse 
=   new  Mouse( " jim " );

            cat.CatShout 
+=  Mouse.ShowMsg;   // 注册静态方法
            cat.CatShout  +=  ( new  Mouse( " tom " )).ShowMsgWithName;   // 给匿名对象注册方法
            cat.CatShout  +=   new  Cat.CatShoutEventHandler(mouse.ShowMsgWithName);
            cat.shout();   
// 猫叫,会自动调用注册过对象的方法
            Console.ReadLine();
        }
    }

}
输出:

有只猫,它的名字是:jack 没有名字 快跑:
它的声音:large sound

有只猫,它的名字是:jack 小老鼠tom 快跑:
它的声音:large sound

有只猫,它的名字是:jack 小老鼠jim 快跑:
它的声音:large sound


 

 

转载于:https://www.cnblogs.com/longfeitengda/archive/2010/03/10/1682177.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值