Interface Events Example

namespace  WrapTwoInterfaceEvents
{
    
using  System;

    
public   interface  IDrawingObject
    {
        
//  Raise this event before drawing
        
//  the object.
         event  EventHandler OnDraw;
    }
    
public   interface  IShape
    {
        
//  Raise this event after drawing
        
//  the shape.
         event  EventHandler OnDraw;
    }


    
//  Base class event publisher inherits two
    
//  interfaces, each with an OnDraw event
     public   class  Shape : IDrawingObject, IShape
    {
        
//  Create an event for each interface event
         event  EventHandler PreDrawEvent;
        
event  EventHandler PostDrawEvent;

        
//  Explicit interface implementation required.
        
//  Associate IDrawingObject's event with
        
//  PreDrawEvent
         event  EventHandler IDrawingObject.OnDraw
        {
            add
            {
                
if  (PreDrawEvent  !=   null )
                {
                    
lock  (PreDrawEvent)
                    {
                        PreDrawEvent 
+=  value;
                    }
                }
                
else
                {
                    PreDrawEvent 
=  value;
                }
            }

            remove
            {

                
if  (PreDrawEvent  !=   null )
                {
                    
lock  (PreDrawEvent)
                    {
                        PreDrawEvent 
-=  value;
                    }
                }
            }
        }
        
//  Explicit interface implementation required.
        
//  Associate IShape's event with
        
//  PostDrawEvent
         event  EventHandler IShape.OnDraw
        {
            add
            {
                
if  (PostDrawEvent  !=   null )
                    
lock  (PostDrawEvent)
                    {
                        PostDrawEvent 
+=  value;
                    }
                
else
                    PostDrawEvent 
=  value;
            }
            remove
            {
                
if  (PostDrawEvent  !=   null )
                    
lock  (PostDrawEvent)
                    {
                        PostDrawEvent 
-=  value;
                    }
            }


        }

        
//  For the sake of simplicity this one method
        
//  implements both interfaces. 
         public   void  Draw()
        {
            
//  Raise IDrawingObject's event before the object is drawn.
            EventHandler handler  =  PreDrawEvent;
            
if  (handler  !=   null )
            {
                handler(
this new  EventArgs());
            }
            Console.WriteLine(
" Drawing a shape. " );

            
//  RaiseIShape's event after the object is drawn.
            handler  =  PostDrawEvent;
            
if  (handler  !=   null )
            {
                handler(
this new  EventArgs());
            }
        }
    }
    
public   class  Subscriber1
    {
        
//  References the shape object as an IDrawingObject
         public  Subscriber1(Shape shape)
        {
            IDrawingObject d 
=  (IDrawingObject)shape;
            d.OnDraw 
+=   new  EventHandler(d_OnDraw);
        }

        
void  d_OnDraw( object  sender, EventArgs e)
        {
            Console.WriteLine(
" Sub1 receives the IDrawingObject event. " );
        }
    }
    
//  References the shape object as an IShape
     public   class  Subscriber2
    {
        
public  Subscriber2(Shape shape)
        {
            IShape d 
=  (IShape)shape;
            d.OnDraw 
+=   new  EventHandler(d_OnDraw);
        }

        
void  d_OnDraw( object  sender, EventArgs e)
        {
            Console.WriteLine(
" Sub2 receives the IShape event. " );
        }
    }


    
public   class  Program
    {
        
static   void  Main( string [] args)
        {
            Shape shape 
=   new  Shape();
            Subscriber1 sub 
=   new  Subscriber1(shape);
            Subscriber2 sub2 
=   new  Subscriber2(shape);
            shape.Draw();

            Console.WriteLine(
" Press Enter to close this window. " );
            Console.ReadLine();
        }
    }

}
posted on 2009-09-29 14:39 進哥 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/euleramon/archive/2009/09/29/1576369.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值