拜托是甚么?你对拜托熟习吗?答案是肯定的!你对拜托1定很熟习!对拜托的熟习,正如你对1个倒立着的人1样熟习!为何这么说?由于方法的调用对每个学习过编程的人来讲都很熟习,而且是熟习的不能再熟习了!那末方法的调用和拜托又有甚么关系呢?其实拜托就是将被动调用改成了主动服务。
拜托其实就是1种思想的转变:由之前的被动调用方法转变成,主动服务于某个方法。将方法间的交互思惟做了1个转变。学习过3层的人不难想象方法之间的调用关系吧,那末使用了拜托以后他们方法之间的关系又是甚么样的呢?(借用两张图展现1下)
图1:是用拜托
图2:未使用拜托
从宏观的角度弄清楚了“拜托”是甚么了。再用1段代码来展现拜托更加细节的1面:
**拜托的定义:
注意视察拜托的定义,不难发现,拜托与类同级。
//定义拜托
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();
}
}
}
总结:
天下之事难易乎?为之,则难者亦易矣;不为,则易者亦难矣。人之为学有难易乎?学之,则难者亦易矣;不学,则易者亦难矣。