星空网 > 软件开发 > ASP.net

設計模式应用之工厂方法模式

對於http請求原理的朋友應該了解IIS上後綴映射

/*当我们请求一个*.aspx的文件时,此时会映射到System.Web.UI.PageHandlerFactory类上进行处理,而对*.ashx的请求将映射到System.Web.UI.SimpleHandlerFactory类中
(这两个类都是继承于IHttpHandlerFactory接口的)*/

下面用一線圖來直觀的看一下

設計模式应用之工厂方法模式

圖引用:http://www.cnblogs.com/zhili/p/FactoryMethod.html

為了更直觀的代碼DEOM我把.net部分源碼弄出來了

/*当我们请求一个*.aspx的文件时,此时会映射到System.Web.UI.PageHandlerFactory类上进行处理,而对*.ashx的请求将映射到System.Web.UI.SimpleHandlerFactory类中(这两个类都是继承于IHttpHandlerFactory接口的)*///抽象工廠public interface IHttpHandlerFactory{  //返回接口---->抽象產品角色  IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated);  void ReleaseHandler(IHttpHandler handler);}//具體工廠internal class SimpleHandlerFactory : IHttpHandlerFactory2, IHttpHandlerFactory{  public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)  {    return ((IHttpHandlerFactory2)this).GetHandler(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path);  }  //其他方法省略掉了.....}//具體工廠public class PageHandlerFactory : IHttpHandlerFactory2, IHttpHandlerFactory{  public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)  {    return this.GetHandlerHelper(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path);  }  //其他方法省略掉了.....}//抽象產品角色public interface IHttpHandler{  bool IsReusable{get;}  void ProcessRequest(HttpContext context);}//頁面aspx(具體頁面產品)public partial class SystemManager_WebsitePublishing_publishServerFileManageList : System.Web.UI.Page{  protected void Page_Load(object sender, EventArgs e)  {       }}public class Page : IHttpHandler{  }/*-------------------------------------------------------*///一般處理程序ashx(具體一般處理程序產品)public class getPublishServerQueueInfo : IHttpHandler{  public void ProcessRequest (HttpContext context) {    }  public bool IsReusable {    get {      return false;    }  }}

//沒有在場景用的小例子:

//抽象产品public interface ILight{  void TurnOn();  void TurnOff();}//具体的产品类:灯泡public class BulbLight:ILight{  public void TurnOn()  {    Console.WriteLine("BulbLight turns on.");  }  public void TurnOff()  {    Console.WriteLine("BulbLight turns off.");  }}// 具体的产品类:灯管public class TubeLight:ILight{  public void TurnOn()  {    Console.WriteLine("TubeLight turns on.");  }  public void TurnOff()  {    Console.WriteLine("TubeLight turns off.");  }  }//抽象的工厂类public interface ICreator{  ILight CreateLight();}//具体灯泡工厂public class BulbCreator:ICreator{  public ILight CreateLight()  {    return new BulbLight();  }}//具体灯管工厂public class TubeCreator:ICreator{  public ILight CreateLight()  {    return new TubeLight();  }}static void Main(string[] args){  //先给我来个灯泡  ICreator creator = new BulbCreator();  ILight light = creator.CreateLight();  light.TurnOn();  light.TurnOff();  //再来个灯管看看  creator = new TubeCreator();  light = creator.CreateLight();  light.TurnOn();  light.TurnOff();}

把上面兩個看一看 比較一下 是不是這麼一回事啊!(注:下面这个例子引用博客园某位博友的代码)

 




原标题:設計模式应用之工厂方法模式

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

美国航线:https://www.goluckyvip.com/tag/11631.html
木制品海运:https://www.goluckyvip.com/tag/11632.html
跨境电商产品:https://www.goluckyvip.com/tag/11633.html
无单放货:https://www.goluckyvip.com/tag/11634.html
国际航运业:https://www.goluckyvip.com/tag/11635.html
美国对台军售:https://www.goluckyvip.com/tag/11636.html
在古巴做游轮 古巴旅游项目:https://www.vstour.cn/a/363194.html
西藏旅游攻略自驾游需要多少天 去西藏旅游自驾游要多久时间:https://www.vstour.cn/a/363195.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流