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

本文详细介绍了IIS服务器如何通过后缀映射机制处理*.aspx和*.ashx文件请求,具体展示了System.Web.UI.PageHandlerFactory和System.Web.UI.SimpleHandlerFactory两个类在处理不同文件类型时的角色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

對於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();
}

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值