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

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

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

 

posted @ 2016-01-07 17:15  KyrieYang  阅读(579)  评论(0编辑  收藏  举报