The first step, the Ajax Operation interface (the purpose is to play an interface detection function)
(1) Introduction of interface file
//define a static method to implement direct validation of interfaces and implementation classes//static method do not write Interface.prototype, because this is written to the interface of the prototype chain//We're going to write the static function directly to the class level .//(1) Define an interface classvarInterface=function (name,methods) {//Name : Interface name if(arguments.length<2) {alert ("must be a two parameter") } This. name=name; This. methods=[];//defines an empty array load function name for(varI=0; i<methods.length;i++){ if(typeofmethods[i]!="string") {alert ("function Name must be a string type"); }Else { This. Methods.push (Methods[i]); }}};interface.ensureimplement=function (Object) { if(arguments.length<2){ Throw NewError ("parameters must be no less than 2") return false; } for(varI=1; i<arguments.length;i++){ varInter=Arguments[i]; //if it is an interface, it must be a interface type . if(inter.constructor!=Interface) { Throw NewError ("if it is an interface class, it must be a interface type."); } //determine whether the methods in the interface are all implemented//Traversal Function Collection for(varj=0; j<inter.methods.length;j++){ varMETHOD=INTER.METHODS[J];//all functions in the interface//Object[method] passed in function//The final decision is whether the incoming function matches the function used in the interface. if(!Object[method]| |typeof Object[method]!="function"){//The implementation class must have a method name that is the same as the method name used in the interface Throw NewError ("all methods in the interface are not fully implemented in the implementation class") } } }}
The second step is to instantiate a specific Ajax interface
var ajaxhandler=New Interface ("ajaxhandler", ["request" ,"createxhrobject"]);
The third step, the Ajax implementation class for the interface
(1) Define an empty class
var Simplehandler=function () {};
(2) Extending the prototype directly on the empty class---Implement the method inside the interface
Simplehandler.prototype={ /** method:get post * URL: Request Address * Callback: callback function * Postvars: Incoming parameter **/request:function (method,url,callback,postvars) {//1, use factory to get XHR object varXhr= This. Createxhrobject (); Xhr.onreadystatechange=function () {//4 means the interaction is complete. if(xhr.readystate!=4)return; //a value of 200 is normal interaction completion//404 File Not Found//500 Internal Program error(xhr.status== $)?callback.success (xhr.responsetext,xhr.responsexml): Callback.failure (Xhr.status); } //Open LinkXhr.open (Method,url,true); //Setting Parameters if(method!="POST") {Postvars=NULL; } xhr.send (Postvars); }, //2, Get Xhr Method--different browser not the samecreatexhrobject:function () {varmethods=[function () {return NewXMLHttpRequest ();}, function () {return NewActiveXObject ("Microsoft.XMLHTTP");} ]; //using Try--catch to make an intelligent loop body for(varI=0; i<methods.length;i++){ Try{methods[i] (); }Catch(e) {Continue;//back to where the loop started. } This. Createxhrobject=methods[i] ();//It is important to ensure that the full-loop array is not used for every request returnMethods[i] (); } //If all is wrong then error Throw NewError ("Error"); } };
The third step, the use of inspection
(1) Instantiating an object
var myhandler=New simplehandler ();
(2) interface verifies that the implementation class is fully implemented in the interface method
Interface.ensureimplement (Myhandler,ajaxhandler); // Verify that all methods in the interface are implemented
(3) Define a callback object
var callback={ success:function (responsetext) { alert ("OK" }, failure:function (status) { alert (status+failure " ) } };
(4) Final use format
Myhandler.request ("POST","http://www.baidu.com", callback ); //If The URL is "" will default to local links, other correct links, there will be cross-domain issues
JavaScript design pattern-Simple Factory Model example---XHR factory