對於http請求原理的朋友應該了解IIS上後綴映射
/*當我們請求一個*.aspx的文件時,此時會映射到System.Web.UI.PageHandlerFactory類上進行處理,而對*.ashx的請求將映射到System.Web.UI.SimpleHandlerFactory類中
(這兩個類都是繼承于IHttpHandlerFactory接口的)*/
下面用一線圖來直觀的看一下
圖引用:http://www.companysz.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();}
把上面兩個看一看 比較一下 是不是這麼一回事啊!(注:下面這個例子引用博客園某位博友的代碼)
|
新聞熱點
疑難解答