JavaScript裝飾者模式原理與用法實(shí)例詳解
本文實(shí)例講述了JavaScript裝飾者模式原理與用法。分享給大家供大家參考,具體如下:
這里我們通過(guò)需求逐漸引出裝飾者模式。
下面是一個(gè)關(guān)于幾代汽車的不同逐漸體現(xiàn)裝飾者模式的。
首先,我們先引入一個(gè)接口文件----目的為檢驗(yàn)實(shí)現(xiàn)類是否完全實(shí)現(xiàn)接口中的方法,代碼如下,
//定義一個(gè)靜態(tài)方法來(lái)實(shí)現(xiàn)接口與實(shí)現(xiàn)類的直接檢驗(yàn) //靜態(tài)方法不要寫出Interface.prototype ,因?yàn)檫@是寫到接口的原型鏈上的 //我們要把靜態(tài)的函數(shù)直接寫到類層次上 //定義一個(gè)接口類 var Interface=function (name,methods) {//name:接口名字 if(arguments.length<2){ alert("必須是兩個(gè)參數(shù)") } this.name=name; this.methods=[];//定義一個(gè)空數(shù)組裝載函數(shù)名 for(var i=0;i<methods.length;i++){ if(typeof methods[i]!="string"){ alert("函數(shù)名必須是字符串類型"); }else { this.methods.push( methods[i]); } } }; Interface.ensureImplement=function (object) { if(arguments.length<2){ throw new Error("參數(shù)必須不少于2個(gè)") return false; } for(var i=1;i<arguments.length;i++){ var inter=arguments[i]; //如果是接口就必須是Interface類型 if(inter.constructor!=Interface){ throw new Error("如果是接口類的話,就必須是Interface類型"); } //判斷接口中的方法是否全部實(shí)現(xiàn) //遍歷函數(shù)集合分析 for(var j=0;j<inter.methods.length;j++){ var method=inter.methods[j];//接口中所有函數(shù) //object[method]傳入的函數(shù) //最終是判斷傳入的函數(shù)是否與接口中所用函數(shù)匹配 if(!object[method]||typeof object[method]!="function" ){//實(shí)現(xiàn)類中必須有方法名字與接口中所用方法名相同 throw new Error("實(shí)現(xiàn)類中沒(méi)有完全實(shí)現(xiàn)接口中的所有方法") } } } }
(1)統(tǒng)一接口
var ShopInterface=new Interface("FirstShop",["getPrice","assemble"]);//規(guī)定了實(shí)現(xiàn)的方法
(2)實(shí)現(xiàn)接口并內(nèi)部檢驗(yàn)
var first=function () { //接口實(shí)現(xiàn)部分 this.getPrice=function () { document.write(15000+"<br>") } this.assemble=function () { document.write("汽車組裝....<br>") } Interface.ensureImplement(this,ShopInterface);//檢驗(yàn)類是否實(shí)現(xiàn)接口 }
(3)第一個(gè)汽車實(shí)例
//第一個(gè)汽車實(shí)例 var firstShop=new first(); firstShop.getPrice(); firstShop.assemble(); document.write("...............first...............<br>")
現(xiàn)在我們開(kāi)始有一個(gè)新的需求,汽車需要有附屬的產(chǎn)品如: 音響(K) ,真皮沙發(fā)(M),保險(xiǎn)杠(N)。
通過(guò)分析我們可以知道,每一個(gè)附屬的產(chǎn)品會(huì)影響到到汽車的組裝和其價(jià)格,那我們能想到什么辦法呢?
第一種方案:通過(guò) 修改接口
(1)接口定義為
var SecondInterface=new Interface("SecondInterface",["getPrice","assemble","addK","addM","addN"]);
(2)類實(shí)現(xiàn)接口并驗(yàn)證
var second=function () { var price=15000; //實(shí)例接口部分 this.getPrice=function () { document.write(price+"<br>") } this.assemble=function () { document.write("汽車組裝.....<br>"); } this.addK=function () { price+=1000; } this.addM=function () { price+=2000; } this.addN=function () { price+=3000; } Interface.ensureImplement(this,SecondInterface);//當(dāng)前對(duì)象實(shí)例時(shí)會(huì)被調(diào)用 }
(3)第二個(gè)汽車實(shí)例
//第二個(gè)汽車實(shí)例 var secondShop=new second(); secondShop.addK(); secondShop.addM(); secondShop.addN(); secondShop.getPrice(); secondShop.assemble(); document.write(".....................second.........................<br>");
咦,我們好像實(shí)現(xiàn)啦,但是問(wèn)題來(lái)了,我把接口改了可是我實(shí)現(xiàn)本接口的是類不一定全要有K,M,N呀。難道我要修改所有實(shí)現(xiàn)本接口的實(shí)現(xiàn)類嗎?顯然是不對(duì)的,如果不改變接口那我就增加子類,這樣可以嗎?
第二種方案,不改變接口,增加子類
(1)接口仍然為
var thirdInterface=new Interface("FirstShop",["getPrice","assemble"]);
(2)汽車原型類--實(shí)現(xiàn)接口
var third=function () { this.getPrice=function () { document.write(15000+"<br>"); } this.assemble=function () { document.write("汽車組裝.....<br>"); } Interface.ensureImplement(this,thirdInterface); }
(3)各個(gè)子類
var thirdM=function () { this.getPrice=function () { document.write(15000+"<br>"); } this.assemble=function () { document.write("汽車組裝.....<br>"); } Interface.ensureImplement(this,thirdInterface); };
我們不禁會(huì)問(wèn),難道每個(gè)子類都要這樣寫嗎?如果子類非常多的話,那我們還不得寫瘋,所以這種方式也是不可取的。
第三種方案:使用裝飾器模式
裝飾者可以為原型對(duì)象添加新的特性,透明的把對(duì)象包裝在具有相同接口的新對(duì)象中。
具體代碼如下:
(1)接口中不變,代碼如下
var comInterface=new Interface("FirstShop",["getPrice","assemble"]);
(2)目標(biāo)對(duì)象(原型)--需要被裝飾的原對(duì)象(屬于包裹在內(nèi)部的部分)--實(shí)現(xiàn)接口并在實(shí)例時(shí)檢驗(yàn)
var targetShop = function(){ this.getPrice = function(){ return 15000; } this.assemble =function(){ document.write("汽車組裝....<br>") } Interface.ensureImplement(this,comInterface);//接口檢驗(yàn) }
(3)各裝飾類,包裹原對(duì)象的東西。
#M:
var carM = function(carShop) { this.getPrice = function () { return 1000 + carShop.getPrice(); } this.assemble = function () { document.write("M組裝....<br>") } Interface.ensureImplement(this,comInterface);//接口檢驗(yàn) }
#N:
var carN = function(carShop){ this.getPrice = function(){ return 2000+carShop.getPrice(); } this.assemble =function(){ document.write("N組裝....<br>") } Interface.ensureImplement(this,comInterface);//接口檢驗(yàn) }
#K:
var carK=function (carShop) { this.getPrice=function () { return 3000+carShop.getPrice(); } this.assemble=function () { document.write("K組裝....<br>") } Interface.ensureImplement(this,comInterface);//接口檢驗(yàn) };
(4)使用各種裝飾來(lái)包裝我們的車吧
//包裝車 var newCar=new carK(new carM(new targetShop));//有K和M的車 var newCar2=new carK(new carM(new carN(new targetShop))); document.write(newCar.getPrice()+"<br>"); document.write(newCar2.getPrice());
總結(jié)一下,裝飾者可以用在類上,同樣也可以用在類中的函數(shù)上。
如果原有的功能不是適合你的項(xiàng)目, 你需要大量的擴(kuò)充原有功能, 并且不不想改變?cè)械慕涌?那你用裝飾者模式就對(duì)了。
用圖理解一下上述模式:
包裝的原理圖:--包裝鏈
感興趣的朋友可以使用在線HTML/CSS/JavaScript前端代碼調(diào)試運(yùn)行工具:http://tools.jb51.net/code/WebCodeRun測(cè)試上述代碼運(yùn)行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容還可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
用最通俗易懂的代碼幫助新手理解javascript閉包 推薦
我同樣也是個(gè)javascript新手,怎么說(shuō)呢,先學(xué)的jquery,精通之后發(fā)現(xiàn)了javascript的重要性,再回過(guò)頭來(lái)學(xué)javascript面向?qū)ο缶幊?/div> 2012-03-03Node.js中AES加密和其它語(yǔ)言不一致問(wèn)題解決辦法
這篇文章主要介紹了Node.js中AES加密和其它語(yǔ)言不一致問(wèn)題解決辦法,例如和C#、JAVA語(yǔ)言相互通信時(shí),需要的朋友可以參考下2014-03-03Bootstrap進(jìn)度條與AJAX后端數(shù)據(jù)傳遞結(jié)合使用實(shí)例詳解
這篇文章主要介紹了Bootstrap進(jìn)度條與AJAX后端數(shù)據(jù)傳遞結(jié)合使用,需要的朋友可以參考下2017-04-04JS中用try catch對(duì)代碼運(yùn)行的性能影響分析
要捕獲JavaScript代碼中的異常一般會(huì)采用 try catch,不過(guò)try catch的使用是否是對(duì)代碼性能產(chǎn)生影響呢?答案是肯定有的,但是有多少不得而知。下面這篇文章就給大家詳細(xì)介紹了在JS中用try catch對(duì)代碼運(yùn)行的性能影響,有需要的朋友們可以參考借鑒。2016-12-12利用JavaScript構(gòu)建樹(shù)形圖的方法詳解
?樹(shù)形圖可視化廣泛用于分層數(shù)據(jù)分析。如果你沒(méi)有經(jīng)驗(yàn)還想創(chuàng)建一個(gè),那將會(huì)有些復(fù)雜。下面是一個(gè)詳細(xì)教程,教你如何使用JavaScript創(chuàng)建交互式樹(shù)形圖2022-06-06跟我學(xué)Nodejs(二)--- Node.js事件模塊
events是node.js 最重要的模塊,events模塊只提供了一個(gè)對(duì)象events.EventEmitter,EventEmitter 的核心是事件發(fā)射與事件監(jiān)聽(tīng)器。2014-05-05最新評(píng)論