Defines an interface for creating objects, so that the subclass determines which class to instantiate.Factory methodDelay the instantiation of a class to its subclass.
To. NET Framework 2.0 system. Web,System. Web. UIFor example
System. Web. ihttphandler
Public Interface Ihttphandler
{
Bool Isreusable { Get ;}
Void Processrequest (httpcontext context );
}
System. Web. ihttphandlerfactory2
Public Interface Ihttphandlerfactory2
{
Ihttphandler gethandler (httpcontext context, string requesttype, virtualpath, string physicalpath );
}
System. Web. UI. Page
Code
Public Class Page: templatecontrol, ihttphandler
{
//
Public Bool Isreusable
{
Get { Return False ;}
}
//
Public Virtual VoidProcessrequest (httpcontext context)
{
//
}
//
}
System. Web. UI. pagehandlerfactory
Code
Public Class Pagehandlerfactory: ihttphandlerfactory2
{
Private Bool _ Isinheritedinstance;
Protected Internal Pagehandlerfactory ()
{
// Check whether this is the exact pagehandlerfactory, or a derived class
_ Isinheritedinstance = (GetType () ! = Typeof (Pagehandlerfactory ));
}
Public Virtual Ihttphandler gethandler (httpcontext context, String Requesttype, String Virtualpath, String Path)
{
Debug. Trace ( " Pagehandlerfactory " , " Pagehandlerfactory: " + Virtualpath );
// This shoshould never get called in ISAPI mode but currently is in integrated mode
// Debug. Assert (false );
Return Gethandlerhelper (context, requesttype, virtualpath. createnonrelative (virtualpath), PATH );
}
Ihttphandler ihttphandlerfactory2.gethandler (httpcontext context, string requesttype,
Virtualpath, string physicalpath)
{
// If it's a derived class, we must call the old (less efficient) gethandler, in
// Case It was overriden
If (_ Isinheritedinstance ){
Return Gethandler (context, requesttype, virtualpath. virtualpathstring, physicalpath );
}
Return Gethandlerhelper (context, requesttype, virtualpath, physicalpath );
}
Public Virtual VoidReleasehandler (ihttphandler handler ){}
Private Ihttphandler gethandlerhelper (httpcontext context, String Requesttype,
Virtualpath, String Physicalpath)
{
Page = Buildmanager. createinstancefromvirtualpath (
Virtualpath, Typeof (Page), context, True /* Allowcrossapp */ , True /* Noassert */ ) As Page;
If (Page = Null )
Return Null ;
Page. templatecontrolvirtualpath=Virtualpath;
ReturnPage;
}
}
DefaultWeb. configConfiguration
Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
<! -- -->
< System. Web >
<! -- -->
< Httphandlers >
<! -- -->
< Add Path = "*. Aspx" Verb = "*" Type = "System. Web. UI. pagehandlerfactory" Validate = "True" />
< Add Path = "*. Ashx" Verb = "*" Type = "System. Web. UI. simplehandlerfactory" Validate = "True" />
< Add Path = "*. Asmx" Verb = "*" Type = "System. Web. Services. Protocols. webservicehandlerfactory, system. Web. Services, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a" Validate = "False" />
<! -- -->
</ Httphandlers >
<! -- -->
</ System. Web >
<! -- -->
</ Configuration >
Ihttphandlerfactory2YesIhttphandlerTo generate differentHttphandlerObject.
httphandler is a real processing center for HTTP requests, in this httphandler container, Asp. net Framework to compile and execute the Server Page requested by the client, append the processed information to HTTP the request information flow is returned to httpmodule . One HTTP request is finally sent to one httphandler processrequest method in the container.
HttphandlerfactoryIsHTTPRequest to arrive at thisHttphandler Factory,HttphandlerfactoryAHttphandlerContainer.HttphandlerContainer to process thisHTTPRequest. ForAspxPage,ASP. NET FrameworkBy defaultSystem. Web. UI. pagehandlerfactoryThisHttphandlerfactory.
The factory method mode is applicable in the following cases:
1.When a class does not know the class of the object it must create.
2.When a class wants its subclass to specify the object it creates.
3.When a class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies.