With the popularity of Ajax, Ajax is now widely used in projects. It can be said that AJAX is widely used. It is difficult to manage because of a large number of Ajax requests. As a result, we often send requests in a unified manner, with a unique page or portal. So there are the following similarCode:
Public Partial Class Ajax: system. Web. UI. Page { Protected Void Page_load ( Object Sender, eventargs e ){ String Comm = request [" Comm "]; If (" Edituserinfo ". Equals (Comm) {edituserinfo ();} Else If (" Adduserinfo ". Equals (Comm) {adduserinfo ();}...... Else If (" Deleteuserinfo ". Equals (Comm) {deleteuserinfo ();} response. End ();} Public Void Edituserinfo (){...... }......}
As the customer's needs change and the second phase of development, there may be more Ajax requests, which will create a huge if... Else if... ... Else if ..., At the same time, each request has
The corresponding operation will cause this class to be too large for management and understanding.
The factory mode is suitable for this scenario. It uses a class to encapsulate the creation logic and the instantiation/configuration options of Customer Code. The UML diagram is as follows (drawing with word is not very standard ):
Example:
1. Create an abstract class ajaxcomm.
Public Abstract ClassAjaxcomm {PublicHttprequest request = httpcontext. Current. request;PublicHttpresponse response = httpcontext. Current. response;Public Abstract VoidExecute ();}
Ii. factory class: Create the corresponding class based on the comm Command requested by the user and perform the corresponding operations.
PublicPartialClassAjax: system. Web. UI. Page {Protected VoidPage_load (ObjectSender, eventargs e ){StringComm = request ["Comm"];
// Create a Class Based on comm. Class command rules: for example, if comm is "adduser", the corresponding class name is "addusercomm" ajaxcomm = (ajaxcomm) Assembly. Load ("Web"). Createinstance ("Web. Ajax."+ Comm +"Comm");If(Ajaxcomm! =Null) {Ajaxcomm. Execute () ;}response. End ();}}
3. Implement the corresponding command class.
Public ClassEditimgcomm: ajaxcomm {Public Override VoidExecute (){...... Response. Write ("Success");}}
# Advantages and disadvantages #
+ merge creation logic and instantiation/configuration options.
+ decouples the customer code from the creation logic.
-if it can be directly instantiated, the design will be complicated.