观察者模式与用户控件之间的互动

什么叫观察者设计模式(Observer Pattern),在此不作过多解释。Insus.NET以一个很简单的例子来演示给大家看看。一个是发布者,而发布者只关心会有谁订阅:

IPublish
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

///   <summary>
///  Summary description for IPublish
///   </summary>
namespace Insus.NET
{   
     // 发布者接口
     public  interface IPublish
    {
        // 只关心会有谁订阅
         void Subscription(ISubscribe obj);
    }
}


另一个是订阅者,而只会关心自己订阅的,是否发布的事情:

ISubscribe
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

///   <summary>
///  Summary description for ISubscribe
///   </summary>
namespace Insus.NET
{
    // 订阅者接口
     public  interface ISubscribe
    {        
         // 订阅者只会关心自己订阅发布的事情
         void Release( string bookName);
    }
}

 下面,Insus.NET列出四个用户,均以用户控件作代表,分别是UserA.ascx,UserB.ascx,UserC.ascx和UserD.ascx ,一旦发布者发布消息之后,订阅者会收到信息,并做出自己的事情。

其中UserA,UserB和UserD作了订阅,也就是实作了ISubscribe接口,而UserC没有订阅,所以它没有实作接口。

UserA.ascx.cs:

UserA.ascx.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public  partial  class UserA : System.Web.UI.UserControl,ISubscribe
{
     protected  void Page_Load( object sender, EventArgs e)
    {

    }

     public  void Release( string bookName)
    {
        Label label =  new Label();
        label.ForeColor = Color.Red;
        label.Text =  " UserA: 我订阅的书 " + bookName +  " 发行了。 ";
         this.PlaceHolder1.Controls.Add(label);
    }
}

UserB.ascx.cs:

UserB.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public  partial  class UserB : System.Web.UI.UserControl, ISubscribe
{
     protected  void Page_Load( object sender, EventArgs e)
    {

    }

     public  void Release( string bookName)
    {
         this.Label1.Visible =  true;
         this.Label1.Text =  " UserB: 马上去书店购买 " + bookName;
    }
}

 

UserD.ascx.cs:

UserD.ascx.cs
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public  partial  class UserD : System.Web.UI.UserControl,ISubscribe
{
     protected  void Page_Load( object sender, EventArgs e)
    {
        
    }

     public  void Release( string bookName)
    {
         this.myInfo.Visible =  true;
         this.Label1.Text = bookName;
    }    
}

 

不管怎样,它们之间必须在一个平台aspx(Default.aspx)之中互动,重点部分有注释。

Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;

public  partial  class _Default : System.Web.UI.Page, IPublish
{
     // 用一个集合,存储订阅者
    List<ISubscribe> _List =  new List<ISubscribe>();
     string bookName =  " 《观察者设计模式诠释》 ";   

     protected  void Page_Load( object sender, EventArgs e)
    {
        // 在这里登记订阅的用户        
         this.Subscription((ISubscribe) this.UserA1);
         this.Subscription((ISubscribe) this.UserB1);       
         this.Subscription((ISubscribe) this.UserD1);
         this.LabelBookName.Text = bookName;
    }

     protected  void Button1_Click( object sender, EventArgs e)
    {
        // 这样好的书,竟然无人订阅!!!
         if (_List.Count ==  0return;

         // 当有订阅者时,把消息发布给他们。
         for ( int i =  0; i < _List.Count; i++)
        {
            ((ISubscribe)_List[i]).Release(bookName);
        }
    }

     public  void Subscription(ISubscribe obj)
    {
        _List.Add(obj);
    }
}

 

 由于UserC没有订阅,因此UserD用户收到发布消息之后,会告诉UserC.但是又由于UserD与UserC没有面对面,只有通过另外一个方式,打电话或委托(电话,或是委托对象,就是Page)的方式来告之了。当UserC收到消息之后,回复答谢,当然也使用相同的方法了。

View Code
  // 由于不能面对面告之,只好使用另外一种方法,如委委托Page去做。
     public  delegate  void UserControlEventHandler( object sender,  string message);

 

 UserD的告诉UserC方法:

View Code
  protected  void Button1_Click( object sender, EventArgs e)
    {
         if (Click !=  null)
        {
             string message =  " Hi,UserC, 有好消息告诉你, "this.Label1.Text +  " 开始有销售了,你也去购买一本吧。 ";
            Click( this, message);
        }

         this.Button1.Visible =  false;
    }


Page对象处理委托的事情:

View Code
  protected  override  void OnInit(EventArgs e)
    {
         base.OnInit(e);
         this.UserD1.Click +=  new UserD.UserControlEventHandler( this.UserC1.Tell);      
            }

 

UserC接收到消息之后,需要处理的事情:

View Code
  public  void Tell( object sender,  string message)
    {
         this.UsercInfo.Visible =  true;
         this.Label1.Text = message;
    }

下面可以看到动画效果:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值