偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

深入理解JavaScript:設(shè)計(jì)模式之組合模式

開(kāi)發(fā) 前端
組合模式(Composite)將對(duì)象組合成樹(shù)形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu),組合模式使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性。詳細(xì)請(qǐng)看下文

介  紹

組合模式(Composite)將對(duì)象組合成樹(shù)形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu),組合模式使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性。

常見(jiàn)的場(chǎng)景有asp.net里的控件機(jī)制(即control里可以包含子control,可以遞歸操作、添加、刪除子control),類似的還有DOM的機(jī)制,一個(gè)DOM節(jié)點(diǎn)可以包含子節(jié)點(diǎn),不管是父節(jié)點(diǎn)還是子節(jié)點(diǎn)都有添加、刪除、遍歷子節(jié)點(diǎn)的通用功能。所以說(shuō)組合模式的關(guān)鍵是要有一個(gè)抽象類,它既可以表示子元素,又可以表示父元素。

正  文

舉個(gè)例子,有家餐廳提供了各種各樣的菜品,每個(gè)餐桌都有一本菜單,菜單上列出了該餐廳所偶的菜品,有早餐糕點(diǎn)、午餐、晚餐等等,每個(gè)餐都有各種各樣的菜單項(xiàng),假設(shè)不管是菜單項(xiàng)還是整個(gè)菜單都應(yīng)該是可以打印的,而且可以添加子項(xiàng),比如午餐可以添加新菜品,而菜單項(xiàng)咖啡也可以添加糖啊什么的。

這種情況,我們就可以利用組合的方式將這些內(nèi)容表示為層次結(jié)構(gòu)了。我們來(lái)逐一分解一下我們的實(shí)現(xiàn)步驟。

***步,先實(shí)現(xiàn)我們的“抽象類”函數(shù)MenuComponent:

  1. var MenuComponent = function () {  
  2. };  
  3. MenuComponent.prototype.getName = function () {  
  4.     throw new Error("該方法必須重寫(xiě)!");  
  5. };  
  6. MenuComponent.prototype.getDescription = function () {  
  7.     throw new Error("該方法必須重寫(xiě)!");  
  8. };  
  9. MenuComponent.prototype.getPrice = function () {  
  10.     throw new Error("該方法必須重寫(xiě)!");  
  11. };  
  12. MenuComponent.prototype.isVegetarian = function () {  
  13.     throw new Error("該方法必須重寫(xiě)!");  
  14. };  
  15. MenuComponent.prototype.print = function () {  
  16.     throw new Error("該方法必須重寫(xiě)!");  
  17. };  
  18. MenuComponent.prototype.add = function () {  
  19.     throw new Error("該方法必須重寫(xiě)!");  
  20. };  
  21. MenuComponent.prototype.remove = function () {  
  22.     throw new Error("該方法必須重寫(xiě)!");  
  23. };  
  24. MenuComponent.prototype.getChild = function () {  
  25.     throw new Error("該方法必須重寫(xiě)!");  
  26. }; 

該函數(shù)提供了2種類型的方法,一種是獲取信息的,比如價(jià)格,名稱等,另外一種是通用操作方法,比如打印、添加、刪除、獲取子菜單。

第二步,創(chuàng)建基本的菜品項(xiàng):

  1. var MenuItem = function (sName, sDescription, bVegetarian, nPrice) {  
  2.     MenuComponent.apply(this);  
  3.     this.sName = sName;  
  4.     this.sDescription = sDescription;  
  5.     this.bVegetarian = bVegetarian;  
  6.     this.nPrice = nPrice;  
  7. };  
  8. MenuItem.prototype = new MenuComponent();  
  9. MenuItem.prototype.getName = function () {  
  10.     return this.sName;  
  11. };  
  12. MenuItem.prototype.getDescription = function () {  
  13.     return this.sDescription;  
  14. };  
  15. MenuItem.prototype.getPrice = function () {  
  16.     return this.nPrice;  
  17. };  
  18. MenuItem.prototype.isVegetarian = function () {  
  19.     return this.bVegetarian;  
  20. };  
  21. MenuItem.prototype.print = function () {  
  22.     console.log(this.getName() + ": " + this.getDescription() + ", " + this.getPrice() + "euros");  
  23. }; 

由代碼可以看出,我們只重新了原型的4個(gè)獲取信息的方法和print方法,沒(méi)有重載其它3個(gè)操作方法,因?yàn)榛静似凡话砑?、刪除、獲取子菜品的方式。

第三步,創(chuàng)建菜品:

  1. var Menu = function (sName, sDescription) {  
  2.     MenuComponent.apply(this);  
  3.     this.aMenuComponents = [];  
  4.     this.sName = sName;  
  5.     this.sDescription = sDescription;  
  6.     this.createIterator = function () {  
  7.         throw new Error("This method must be overwritten!");  
  8.     };  
  9. };  
  10. Menu.prototype = new MenuComponent();  
  11. Menu.prototype.add = function (oMenuComponent) {  
  12.     // 添加子菜品  
  13.     this.aMenuComponents.push(oMenuComponent);  
  14. };  
  15. Menu.prototype.remove = function (oMenuComponent) {  
  16.     // 刪除子菜品  
  17.     var aMenuItems = [];  
  18.     var nMenuItem = 0;  
  19.     var nLenMenuItems = this.aMenuComponents.length;  
  20.     var oItem = null;  
  21.  
  22.     for (; nMenuItem < nLenMenuItems; ) {  
  23.         oItem = this.aMenuComponents[nMenuItem];  
  24.         if (oItem !== oMenuComponent) {  
  25.             aMenuItems.push(oItem);  
  26.         }  
  27.         nMenuItem = nMenuItem + 1;  
  28.     }  
  29.     this.aMenuComponents = aMenuItems;  
  30. };  
  31. Menu.prototype.getChild = function (nIndex) {  
  32.     //獲取指定的子菜品  
  33.     return this.aMenuComponents[nIndex];  
  34. };  
  35. Menu.prototype.getName = function () {  
  36.     return this.sName;  
  37. };  
  38. Menu.prototype.getDescription = function () {  
  39.     return this.sDescription;  
  40. };  
  41. Menu.prototype.print = function () {  
  42.     // 打印當(dāng)前菜品以及所有的子菜品  
  43.     console.log(this.getName() + ": " + this.getDescription());  
  44.     console.log("--------------------------------------------");  
  45.  
  46.     var nMenuComponent = 0;  
  47.     var nLenMenuComponents = this.aMenuComponents.length;  
  48.     var oMenuComponent = null;  
  49.  
  50.     for (; nMenuComponent < nLenMenuComponents; ) {  
  51.         oMenuComponent = this.aMenuComponents[nMenuComponent];  
  52.         oMenuComponent.print();  
  53.         nMenuComponent = nMenuComponent + 1;  
  54.     }  
  55. }; 

注意上述代碼,除了實(shí)現(xiàn)了添加、刪除、獲取方法外,打印print方法是首先打印當(dāng)前菜品信息,然后循環(huán)遍歷打印所有子菜品信息。

第四步,創(chuàng)建指定的菜品:

我們可以創(chuàng)建幾個(gè)真實(shí)的菜品,比如晚餐、咖啡、糕點(diǎn)等等,其都是用Menu作為其原型,代碼如下:

  1. var DinnerMenu = function () {  
  2.     Menu.apply(this);  
  3. };  
  4. DinnerMenu.prototype = new Menu();  
  5.  
  6. var CafeMenu = function () {  
  7.     Menu.apply(this);  
  8. };  
  9. CafeMenu.prototype = new Menu();  
  10.  
  11. var PancakeHouseMenu = function () {  
  12.     Menu.apply(this);  
  13. };  
  14. PancakeHouseMenu.prototype = new Menu(); 

第五步,創(chuàng)建最***的菜單容器——菜單本:

  1. var Mattress = function (aMenus) {  
  2.     this.aMenus = aMenus;  
  3. };  
  4. Mattress.prototype.printMenu = function () {  
  5.     this.aMenus.print();  
  6. }; 

該函數(shù)接收一個(gè)菜單數(shù)組作為參數(shù),并且值提供了printMenu方法用于打印所有的菜單內(nèi)容。

第六步,調(diào)用方式:

  1. var oPanCakeHouseMenu = new Menu("Pancake House Menu""Breakfast");  
  2. var oDinnerMenu = new Menu("Dinner Menu""Lunch");  
  3. var oCoffeeMenu = new Menu("Cafe Menu""Dinner");  
  4. var oAllMenus = new Menu("ALL MENUS""All menus combined");  
  5.  
  6. oAllMenus.add(oPanCakeHouseMenu);  
  7. oAllMenus.add(oDinnerMenu);  
  8.  
  9. oDinnerMenu.add(new MenuItem("Pasta""Spaghetti with Marinara Sauce, and a slice of sourdough bread"true, 3.89));  
  10. oDinnerMenu.add(oCoffeeMenu);  
  11.  
  12. oCoffeeMenu.add(new MenuItem("Express""Coffee from machine"false, 0.99));  
  13.  
  14. var oMattress = new Mattress(oAllMenus);  
  15. console.log("---------------------------------------------");  
  16. oMattress.printMenu();  
  17. console.log("---------------------------------------------"); 

熟悉asp.net控件開(kāi)發(fā)的同學(xué),是不是看起來(lái)很熟悉?

總  結(jié)

組合模式的使用場(chǎng)景非常明確:

  1. 你想表示對(duì)象的部分-整體層次結(jié)構(gòu)時(shí);
  2. 你希望用戶忽略組合對(duì)象和單個(gè)對(duì)象的不同,用戶將統(tǒng)一地使用組合結(jié)構(gòu)中的所有對(duì)象(方法)

另外該模式經(jīng)常和裝飾者一起使用,它們通常有一個(gè)公共的父類(也就是原型),因此裝飾必須支持具有add、remove、getChild操作的 component接口。

原文鏈接:http://www.cnblogs.com/TomXu/archive/2012/04/12/2435530.html

【編輯推薦】

  1. 深入理解JavaScript:設(shè)計(jì)模式之適配器模式
  2. JavaScript和Dojo引起瀏覽器內(nèi)存泄露問(wèn)題
  3. 在Eclipse中使用JSHint檢查JavaScript
  4. 分享5個(gè)***的JavaScript日期處理類庫(kù)
  5. JavaScript表格組件加載速度測(cè)試
責(zé)任編輯:林師授 來(lái)源: 湯姆大叔的博客
相關(guān)推薦

2012-04-12 09:33:02

JavaScript

2021-02-17 11:25:33

前端JavaScriptthis

2021-09-16 06:44:05

組合模式設(shè)計(jì)

2015-09-08 13:39:10

JavaScript設(shè)計(jì)模式

2012-02-29 09:41:14

JavaScript

2011-09-01 13:51:52

JavaScript

2012-12-25 09:38:41

JavaScript設(shè)計(jì)模式

2024-07-30 11:29:09

2015-11-04 09:57:18

JavaScript原型

2013-11-26 15:48:53

Android設(shè)計(jì)模式SDK

2017-08-08 09:15:41

前端JavaScript頁(yè)面渲染

2019-11-05 10:03:08

callback回調(diào)函數(shù)javascript

2024-07-18 10:12:04

2013-11-05 13:29:04

JavaScriptreplace

2019-08-16 10:46:46

JavaScript工廠模式抽象工廠模式

2012-01-05 15:07:11

JavaScript

2018-12-27 12:34:42

HadoopHDFS分布式系統(tǒng)

2019-03-18 09:50:44

Nginx架構(gòu)服務(wù)器

2014-12-04 14:01:54

openstacknetworkneutron

2023-11-02 21:11:11

JavaScript設(shè)計(jì)模式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)