Jie

心若无尘,一花一世界,一鸟一天堂;心若静,已如涅磐,风声物语,皆可成言.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

委托与事件

Posted on 2009-04-12 14:00  JieNet  阅读(330)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sample_CUI
{
    
namespace Delegate_And_Event
    {
        
/* 场景
        XXX收到一个解决方案(内网建设方案),是否采用这个方案需要经过2个过程。
        第一个过程(资料确认):需要技术部中User1和User2确认。
        第二个过程(最终确认):需要CTO和CEO确认。

        PS:过程中的确认人数和来自的部门也不同,过程有进展要通知相关的人员进行确认。
        
*/

        
/// <summary>
        
/// 通知事件
        
/// </summary>
        public class WorkNoticeEventArgs : EventArgs
        {
            
/// <summary>
            
/// 要通知的人
            
/// </summary>
            public readonly List<string> users;

            
public WorkNoticeEventArgs(List<string> users)
            {
                
this.users = users;
            }
        }

        
/// <summary>
        
/// 确认委托
        
/// </summary>
        
/// <param name="sender">确认方法</param>
        
/// <param name="e">通知事件</param>
        public delegate void WorkNoticeEventHandler(object sender,WorkNoticeEventArgs e);


        
/// <summary>
        
/// 资料确认
        
/// </summary>
        public class ProcessDataValidation
        {
            
//声明事件
            public event WorkNoticeEventHandler Begin = null;
            
public event WorkNoticeEventHandler User1Validated = null;
            
public event WorkNoticeEventHandler User2Validated = null;
            
public event WorkNoticeEventHandler Finished = null;

            
//是否确认
            private bool user1Validated, user2Validated;

            
#region Method

            
/// <summary>
            
/// 过程开始
            
/// </summary>
            
/// <param name="users"></param>
            public virtual void OnBegin(List<string> users)
            {
                
//do something
                Console.WriteLine("Begin[{0}]",
                    
this.ToString().Substring(this.ToString().LastIndexOf('.'+ 1)
                    );
                
if (Begin != null)
                {
                    WorkNoticeEventArgs e 
= new WorkNoticeEventArgs(users);
                    Begin(
this,e);//调用订阅此事件的方法
                }
            }

            
/// <summary>
            
/// User1确认
            
/// </summary>
            
/// <param name="users"></param>
            public virtual void OnUser1Validate(List<string> users)
            {
                
//do something
                user1Validated = true;
                Console.WriteLine(
"Use1 has been validated[{0}]",
                    
this.ToString().Substring(this.ToString().LastIndexOf('.'+ 1)
                    );
                WorkNoticeEventArgs e 
= new WorkNoticeEventArgs(users);
                User1Validated(
this, e);//调用订阅此事件的方法
            }

            
/// <summary>
            
/// User2确认
            
/// </summary>
            
/// <param name="users"></param>
            public virtual void OnUser2Validate(List<string> users)
            {
                
//do something
                user2Validated = true;
                Console.WriteLine(
"Use2 has been validated[{0}]",
                    
this.ToString().Substring(this.ToString().LastIndexOf('.'+ 1)
                    );
                WorkNoticeEventArgs e 
= new WorkNoticeEventArgs(users);
                User2Validated(
this, e);//调用订阅此事件的方法
            }

            
/// <summary>
            
/// 过程完成
            
/// </summary>
            
/// <param name="users"></param>
            public virtual void OnFinish(List<string> users)
            {
                
if (user1Validated && user2Validated)
                {
                    
//do something
                    Console.WriteLine("Finish[{0}]",
                        
this.ToString().Substring(this.ToString().LastIndexOf('.'+ 1)
                        );
                    WorkNoticeEventArgs e 
= new WorkNoticeEventArgs(users);
                    Finished(
this,e);//调用订阅此事件的方法
                }
            }

            
#endregion

        }
//class ProcessDataValidation


        
/// <summary>
        
/// 资料确认子类
        
/// </summary>
        public class ProcessDataValidate_NonNetSolution : ProcessDataValidation
        {
            
/// <summary>
            
/// 改写父类方法
            
/// </summary>
            
/// <param name="users"></param>
            public override void OnBegin(List<string> users)
            {
                
//do something
                users.Clear();
                users.Add(
"Jie");
                
base.OnBegin(users);
            }
        }
//class ProcessDataValidate_NonNetSolution

        
/// <summary>
        
/// 工作通知 监视者
        
/// </summary>
        public class WorkNoticeObserver
        {
            
public void NoticeUsersWhenProcessBegin(object sender, WorkNoticeEventArgs e)
            {
                Console.Write(
"Notice:");
                
for (int i = 0; i < e.users.Count; i++)
                {
                    Console.Write(
"{0} ", e.users[i]);
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            
public void NoticeUsersWhenUser1Validated(object sender, WorkNoticeEventArgs e)
            {
                Console.Write(
"Notice:");
                
for (int i = 0; i < e.users.Count; i++)
                {
                    Console.Write(
"{0} ", e.users[i]);
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            
public void NoticeUsersWhenUser2Validated(object sender, WorkNoticeEventArgs e)
            {
                Console.Write(
"Notice:");
                
for (int i = 0; i < e.users.Count; i++)
                {
                    Console.Write(
"{0} ", e.users[i]);
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            
public void NoticeUsersWhenProcessFinished(object sender, WorkNoticeEventArgs e)
            {
                Console.Write(
"Notice:");
                
for (int i = 0; i < e.users.Count; i++)
                {
                    Console.Write(
"{0} ", e.users[i]);
                }
                Console.WriteLine();
                Console.WriteLine();
            }

        }
//class WorkNoticeObserver


        
public class SampleTest
        {
            
public static void Test()
            {
                List
<string> list1 = new List<string>();
                list1.Add(
"User1");
                list1.Add(
"User2");
                list1.Add(
"CTO");
                list1.Add(
"CEO");

                List
<string> list2 = new List<string>();
                list2.Add(
"User2");

                List
<string> list3 = new List<string>();
                list3.Add(
"User1");

                List
<string> list4 = new List<string>();
                list4.Add(
"CTO");
                list4.Add(
"CEO");

                WorkNoticeObserver wn 
= new WorkNoticeObserver();
                ProcessDataValidation p 
= new ProcessDataValidation();
                
//注册事件
                p.Begin += new WorkNoticeEventHandler(wn.NoticeUsersWhenProcessBegin);
                p.User1Validated 
+= new WorkNoticeEventHandler(wn.NoticeUsersWhenUser1Validated);
                p.User2Validated 
+= new WorkNoticeEventHandler(wn.NoticeUsersWhenUser2Validated);
                p.Finished 
+= new WorkNoticeEventHandler(wn.NoticeUsersWhenProcessFinished);

                
//触发事件
                p.OnBegin(list1);
                p.OnUser1Validate(list2);
                p.OnUser2Validate(list3);
                p.OnFinish(list4);

                p 
= null;
                p 
= new ProcessDataValidate_NonNetSolution();
                
// 子类Begin事件的实现
                p.Begin += delegate(object sender, WorkNoticeEventArgs e) {
                    Console.Write(
"Notice:");
                    
for (int i = 0; i < e.users.Count; i++)
                    {
                        Console.Write(
"{0} ", e.users[i]);
                    }
                    Console.WriteLine();
                };

                p.OnBegin(list2);
            }

        }
//class SampleTest

    }
//namespace Delegate_And_Event
}