标签:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <script> /*职责链模式 所有对象依次处理请求,1不能处理传给2,2不能处理传给3....*/ /*场景 打折 100以下不打折, 100-200 9折 200-400 8折*/ /*好处 处理对象分离,降低耦合度*/ function hander(number){ var secHd = new handle9Discount(); var thirHd = new handle8Discount(thirHd); var hd= new handleDisCount(secHd).disPrice(number); } function handleDisCount(nextHander) { var me = this; this.nextHander = nextHander; this.disPrice = function(number) { if (number < 100) { alert("已处理,原件出售:" + number); } else { me.nextHander.disPrice(number); } } } function handle9Discount(nextHander) { var me = this; this.disPrice = function(number) { if (100 < number && number < 200) { alert("已处理9折销售" + (number * 0.9)) } else { me.nextHander.disPrice(number); } } } function handle8Discount(nextHander) { var me = this; this.disPrice = function(number) { if (200 < number && number < 400) { alert("已处理8折销售" + (number * 0.8)) } else { alert("无法处理!"); } } } hander(199); </script> <body> </body> </html>
标签:
原文地址:http://www.cnblogs.com/yqlog/p/5568146.html