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

标准结构:

.Net Framework中使用的模式-Factory模式_第1张图片

WebRequest结构:

.Net Framework中使用的模式-Factory模式_第2张图片

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

你可能感兴趣的:(framework)