DOTNET framework source code mode (7) -- factory method (factory Mode)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.