zoukankan      html  css  js  c++  java
  • 机房收费系统合作版(五)——初识托付

    托付是什么?你对托付熟悉吗?答案是肯定的!你对托付一定非常熟悉!

    对托付的熟悉,正如你对一个倒立着的人一样熟悉!为什么这么说?由于方法的调用对于每个学习过编程的人来说都非常熟悉,并且是熟悉的不能再熟悉了!那么方法的调用和托付又有什么关系呢?事实上托付就是将被动调用改为了主动服务。

     

    托付事实上就是一种思想的转变:由曾经的被动调用方法转变为,主动服务于某个方法。将方法间的交互思维做了一个转变。学习过三层的人不难想象方法之间的调用关系吧,那么使用了托付之后他们方法之间的关系又是什么样的呢?(借用两张图展示一下)


    图一:是用托付

     

    图二:未使用托付


     

    从宏观的角度弄清楚了“托付”是什么了。

    再用一段代码来展示托付更加细节的一面:

    **托付的定义:

    注意观察托付的定义,不难发现,托付与类同级。

    //定义托付
    public delegate void AddEventHandler<T>(T t);
    public delegate void DeleteEventHandler<T>(T t);
    public delegate void UpdateEventHandler<T>(T t);
    public delegate void QueryEventHandler<T>(T t);
    public delegate T IsExistsEventHandler<T>(T t);
    

    **托付的事件:

    注意观察事件的定义,不难发现。事件与方法同级

    //定义事件
    public event AddEventHandler<T> AddEvent;
    public event DeleteEventHandler<T> DeleteEvent;
    public event UpdateEventHandler<T> UpdateEvent;
    public event QueryEventHandler<T> QueryEvent;
    public event IsExistsEventHandler<T> IsExistsEvent;
    

    **将方法与托付绑定:

    += new AddEventHandler<T>(this.manager.Add);
    += new DeleteEventHandler<T>(this.manager.Delete);
    += new UpdateEventHandler<T>(this.manager.Update);
    += new QueryEventHandler<T>(this.manager.Query);
    += new IsExistsEventHandler<T>(this.manager.isExist);
    

    **为事件装载托付:

    this.AddEvent += new AddEventHandler<T>(this.manager.Add);
    this.DeleteEvent += new DeleteEventHandler<T>(this.manager.Delete);
    this.UpdateEvent += new UpdateEventHandler<T>(this.manager.Update);
    this.QueryEvent += new QueryEventHandler<T>(this.manager.Query);
    this.IsExistsEvent += new IsExistsEventHandler<T>(this.manager.isExist);<strong style="color: rgb(51, 51, 255);">
    </strong>

    **整个登录窗口的外观层代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Login.Model.Entity.Base;
    using Login.Model.Adapter;
    using Login.Model.Entity;
    using System.Reflection;
    namespace Login.Controller
    {
        //定义托付
        public delegate void AddEventHandler<T>(T t);
        public delegate void DeleteEventHandler<T>(T t);
        public delegate void UpdateEventHandler<T>(T t);
        public delegate void QueryEventHandler<T>(T t);
        public delegate T IsExistsEventHandler<T>(T t);
    
        public class FacadeManager<T, TCollection>
        {
            private superManager<T,TCollection> manager;
            
            //定义事件
            public event AddEventHandler<T> AddEvent;
            public event DeleteEventHandler<T> DeleteEvent;
            public event UpdateEventHandler<T> UpdateEvent;
            public event QueryEventHandler<T> QueryEvent;
            public event IsExistsEventHandler<T> IsExistsEvent;
    
            public FacadeManager(T t)
            {
                this.manager = this.createManager(t);
    
                this.AddEvent += new AddEventHandler<T>(this.manager.Add);
                this.DeleteEvent += new DeleteEventHandler<T>(this.manager.Delete);
                this.UpdateEvent += new UpdateEventHandler<T>(this.manager.Update);
                this.QueryEvent += new QueryEventHandler<T>(this.manager.Query);
                this.IsExistsEvent += new IsExistsEventHandler<T>(this.manager.isExist);
            }
            
            /// <summary>
            /// 创建相应的Manager
            /// </summary>
            /// <param name="t"></param>
            /// <returns></returns>
            public superManager<T,TCollection> createManager(T t){
    
                string className = typeof(T).ToString() + "Manager";
                className = className.Substring(className.LastIndexOf('.')+1);
                //return (superManager<T>)Assembly.Load("Login").CreateInstance("Login.Controller." + className);
                //參数数组
                Object[] o =new Object[1];
                o[0]=t;
                return (superManager<T,TCollection>)Assembly.Load("Login").CreateInstance("Login.Controller." + className, false, System.Reflection.BindingFlags.Default, null, o, null, null);
    
            }
            
            /// <summary>
            /// 推断是否存在
            /// </summary>
            /// <param name="t"></param>
            /// <returns></returns>
            public T isExist(T t)
            {
                //return manager.isExist(t);
                return this.IsExistsEvent(t);
            }
    
            public bool add(T t)
            {
                throw new NotImplementedException();
            }
    
            public bool update(T t)
            {
                throw new NotImplementedException();
            }
    
            public bool delete(T t)
            {
                throw new NotImplementedException();
            }
        }
    }


    总结:

    天下之事难易乎?为之,则难者亦易矣;不为,则易者亦难矣。

    人之为学有难易乎?学之,则难者亦易矣;不学,则易者亦难矣。

  • 相关阅读:
    [问题2014S06] 解答
    [问题2014S07] 复旦高等代数II(13级)每周一题(第七教学周)
    [问题2014S05] 解答
    [问题2014S04] 解答
    [问题2014S06] 复旦高等代数II(13级)每周一题(第六教学周)
    [问题2014S03] 解答
    [问题2014S05] 复旦高等代数II(13级)每周一题(第五教学周)
    [问题2014S02] 解答
    [问题2014S04] 复旦高等代数II(13级)每周一题(第四教学周)
    [问题2014S01] 解答
  • 原文地址:https://www.cnblogs.com/lytwajue/p/6963729.html
Copyright © 2011-2022 走看看