ntwo

导航

.Net Framework中使用的模式-Factory模式

标准结构:

未命名图片

WebRequest结构:

2010-11-02 16 14 50

在WebRequst中,不存在独立的Factory类,而是将创建方法做为产品基类的静态方法。这是一种工厂方法的常见变形。Create方法伪码:

public abstract class WebRequest

{
    public static WebRequest Create(string url)
    {
        if(url.BeginWith(“file://”))
            return new FileWebRequest();
        if(url.BeginWith(“ftp://”))
            return new FtpRequest();
        if(url.BeginWith(“file://”))
            return new HttpWebRequest();
    }
}

posted on 2010-11-02 16:23  9527  阅读(231)  评论(0编辑  收藏  举报