Exemplifying the Factory Method Pattern inside the .NET Framework
page 3 of 6
by Xianzhong Zhu
Average Rating: 
Views (Total / Last 10 Days): 37921/ 97

Exploring the Design Pattern of class WebRequest inside the .NET Framework

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.

Listing 7

WebRequest wr= WebRequest.Create(http://www.aspalliance.com);

After that, you can get the WebResponse object according to the newly-created WebRequest object.

Listing 8

WebResponse myresponse=wr.GetResponse();

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.

Listing 9

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.

Listing 10

internal class HttpRequestCreator : IWebRequestCreate
    public WebRequest Create(Uri Uri)
        return new HttpWebRequest(Uri, null);

Another example relates to class FileWebRequestCreator:

internal class FileWebRequestCreator : IWebRequestCreate
    internal FileWebRequestCreator()
    { }
    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 WebRequest

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.

View Entire Article

User Comments

Title: Images missing   
Name: James
Date: 2013-01-21 4:01:32 PM
Hey all figure images are missing. Can you fix this??
Title: Amazing   
Name: Nimesh
Date: 2010-08-04 9:11:39 PM
Hi Xianzhong Zhu,
I can stop praising you for this article. It has answered so many of my questions along the way of learning new things. You should never stop writing such interesting articles man.

Carry on mate,


Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-07 7:24:52 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search