I. Definition
Responsibility Chain Mode: enables multiple objects to have the opportunity to process the request, thus avoiding the coupling between the sender and the recipient of the request, connecting the objects into a chain and passing the request along the chain until an object is processed.
Second, the example
- Suppose such a scene:
- We are responsible for a mobile phone sales of the website, after the payment of 500 yuan deposit and 200 yuan deposit two rounds of booking, to the formal purchase stage. For scheduled users to implement concessions, paid 500 yuan deposit users will receive 100 Yuan Mall coupons, paid a deposit of 200 yuan users will receive 50 yuan of the mall coupons, no payment of the deposit of the user classified as ordinary purchase, and in the case of limited inventory is not guaranteed to buy.
///ordertype:[1:500, 2:200, 3: Ordinary],ispaid:true/false,stock: inventory var order = function (OrderType, ispaid, stock)
{if (OrderType = 1) {if (Ispaid) {Console.log ("500 yuan deposit advance, get 100 coupon");
else {if (Stock > 0) {console.log ("Ordinary Purchase, no coupon");
}else {console.log ("insufficient stock");
}}else if (OrderType = = 2) {if (Ispaid) {console.log ("200 yuan deposit pre-order, get 50 coupon");
else {if (Stock > 0) {console.log ("Ordinary Purchase, no coupon");
}else {console.log ("insufficient stock");
}}else if (OrderType = = 2) {if (Stock > 0) {console.log ("Ordinary Purchase, no coupon");
}else {console.log ("insufficient stock");
The order (1, True, 500)}}; /* Responsibility Chain */var order500 = function (OrderType, ispaid, stock) {if (OrderType = = 1 && Ispaid = True) {Consol
E.log ("500 yuan deposit pre-order, get 100 coupons");
}else {return "nextsuccessor";
}
}; var order200 = function (OrderType, ispaid, stock) {if (OrderType = = 2 && Ispaid = =true) {Console.log ("200 yuan deposit pre-order, get 50 coupons");
}else {return "nextsuccessor";
}
};
var ordernormal = function (OrderType, ispaid, stock) {if (Stock > 0) {console.log ("Ordinary Purchase, no coupon");
}else {console.log ("insufficient stock");
}
};
Function.prototype.after = function (fn) {var self = this;
return function () {var ret = self.apply (this, arguments);
if (ret = = "Nextsuccessor") {return fn.apply (this, arguments);
return ret;
};
var order = Order500.after (order200). After (Ordernormal);
Order (1, true, 10);
Advantage: Decoupling the complex relationship between the requesting sender and the n recipient.
Disadvantage: There is no guarantee that a request will be handled by the nodes in the chain.
Iii. Example: File Upload Object
Example 2: Obtaining a File Upload object with the responsibility chain pattern
PS: Comparing the iterator pattern of learning JavaScript design Patterns
function Getactiveuploadobj () {
try{return
new Activeobject ("Txftnactivex.ftnupload");//IE Upload Control
}catch (e) {return
"Nextsuccessor";
}
}
function Getflashuploadobj () {
if (Supportflash (). F = = 1) { //Supportflash See "javascript design mode-iterator mode"
var str = ' <object type= ' application/x-shockwave-flash ' ></object> ';
return $ (str). Appendto ($ (' body '));
}
return "Nextsuccessor";
}
function Getformuploadobj () {
var str = ' <input name= ' file ' type= ' file ' class= ' ui-file '/> ';
return $ (str). Appendto ($ (' body '));
}
var getuploadobj = Getactiveuploadobj.after (getflashuploadobj). After (getformuploadobj);
Console.log (Getuploadobj ());
Whether the event bubbles in the scope chain, the prototype chain, or the DOM node, we can find the shadow of the responsibility chain.
The above is all in this article and I hope this article will help you learn about JavaScript programming.