The Factory Method pattern is widely used in the design of .NET
framework. As one of the most important classes in processing web requests, WebRequest
plays the key role. It is able to create the corresponding web requests according
to the passed URI objects. For example, when the prefix of the URI is "https://"
or "http://," it can create and return a HttpWebRequest object; in
the case that the prefix is "file://," however, it can create and return
a FileWebRequest object. Both HttpWebRequest and FileWebRequest are derived classes
of class WebRequest. Figure 6 illustrates their relationships.
Figure 6 - The WebRequest class and its two derivatives
There are many means for you to create an instance of class WebRequest.
For example, you can directly leverage the static method Create of this class.
WebRequest wr= WebRequest.Create(http://www.aspalliance.com);
After that, you can get the WebResponse object according to the
newly-created WebRequest object.
It seems that the static Create() method is one of the
implementations of the simple Factory pattern, which can judge the type of class
WebRequest according to the arguments passed to the method, and then create and
return the concrete WebRequest object. If so, the simplest solution is to judge
the value of the argument to decide the type of the object to create using a conditional
sentence. Obviously, this is not a good means, which directly results in the concrete
subclasses of class WebRequest dependent of the static method Create(). In this
way, once it requires increasing the subclasses of class WebRequest, we have to
modify the Create method everywhere.
As you may have imaged, Microsoft architects of .NET Framework
have already grasped the idea of the design pattern. Thus, they introduce the Factory
Method pattern to overcome the above deficiency. They built up a special factory
interface named IWebRequestCreate responsible to create the WebRequest object, which
merely declares one method named Create.
public interface IWebRequestCreate
WebRequest Create(Uri uri);
Corresponding to different kinds of web requests, the Factory
Method pattern can provide different related factory classes which all implement
interface IWebRequestCreate, such as HttpRequestCreator below.
internal class HttpRequestCreator : IWebRequestCreate
public WebRequest Create(Uri Uri)
return new HttpWebRequest(Uri, null);
Another example relates to class FileWebRequestCreator:
internal class FileWebRequestCreator : IWebRequestCreate
public WebRequest Create(Uri uri)
return new FileWebRequest(uri);
So, as you have seen, .NET Framework has created an excellent
factory class architecture that parallels to each product class. Figure 7 indicates
the diagram introduced by the Factory Method pattern.
Figure 7 - The class diagram related to class
Now, you see, whether for the factory classes or the product
classes, there only exist weak dependencies between interface IWebRequestCreate
and the abstract class WebRequest. This appropriately reflects the essence of abstract
or interface oriented programming in the idea of OOP.