javascript設計模式 ? 享元模式原理與用法實例分析
本文實例講述了javascript設計模式 ? 享元模式原理與用法。分享給大家供大家參考,具體如下:
介紹:在我們日常開發中需要創建很多對象,雖然垃圾回收機制能幫我們進行回收,但是在一些需要重復創建對象的場景下,就需要有一種機制來進行優化,提高系統資源的利用率。
享元模式就是解決這類問題,主要目的是減少創建對象的數量。享元模式提倡重用現有同類對象,如未找到匹配的對象則創建新對象
定義:運用共享技術有效的支持大量細粒度對象的復用。系統只適用少量的對象,而這些對象都很相似,狀態變化很小,可以實現對象的多次復用。由于享元模式要求能夠共享的對象必須是細粒度的對象,因此他又稱為輕量級模式,是一種對象結構型模式。
場景:我們以創建圓形對象為例,通過兩個例子來對比享元模式的效果。
示例:
var redCricle = new Circle(’red’);redCricle.setAttr(10,10,10);redCricle.draw(); var redCricle1 = new Circle(’red’);redCricle1.setAttr(1,1,100);redCricle1.draw(); var redCricle2 = new Circle(’red’);redCricle2.setAttr(5,5,50);redCricle2.draw(); var blueCricle = new Circle(’blue’);blueCricle.setAttr(1,1,50);blueCricle.draw(); var blueCricle1 = new Circle(’blue’);blueCricle1.setAttr(12,12,50);blueCricle1.draw(); var blueCricle2 = new Circle(’blue’);blueCricle2.setAttr(2,12,20);blueCricle2.draw();// 創建了一個對象// 畫圓: 顏色:red x:10 y:10 radius:10// 創建了一個對象// 畫圓: 顏色:red x:1 y:1 radius:100// 創建了一個對象// 畫圓: 顏色:red x:5 y:5 radius:50// 創建了一個對象// 畫圓: 顏色:blue x:1 y:1 radius:50// 創建了一個對象// 畫圓: 顏色:blue x:12 y:12 radius:50// 創建了一個對象// 畫圓: 顏色:blue x:2 y:12 radius:20
這種情況下每次使用都需要實例化一次Circle對象,對系統資源來說是一種浪費。
觀察下不難發現,除了第一次需要實例化,其余的可以基于實例繼續修改。
我們修改下:
var Circle = function(color){ console.log(’創建了一個對象’); this.color = color; this.x; this.y; this.radius; this.setAttr = function(x, y, radius){ this.x = x; this.y = y; this.radius = radius; } this.draw = function(){ console.log(’畫圓: 顏色:’ + this.color + ’ x:’ + this.x + ’ y:’ + this.y + ’ radius:’ + this.radius) }} var ShapeFactory = function(){ this.circleMap = {}; this.getCircle = function(color){ var circle = this.circleMap[color]; if(!circle){ circle = new Circle(color); this.circleMap[color] = circle; } return circle; }}var factory = new ShapeFactory(); var redCricle = factory.getCircle(’red’);redCricle.setAttr(10,10,10);redCricle.draw(); var redCricle1 = factory.getCircle(’red’);redCricle1.setAttr(1,1,100);redCricle1.draw(); var redCricle2 = factory.getCircle(’red’);redCricle2.setAttr(5,5,50);redCricle2.draw(); var blueCricle = factory.getCircle(’blue’); blueCricle.setAttr(1,1,50);blueCricle.draw(); var blueCricle1 = factory.getCircle(’blue’);blueCricle1.setAttr(12,12,50);blueCricle1.draw(); var blueCricle2 = factory.getCircle(’blue’);blueCricle2.setAttr(2,12,20);blueCricle2.draw(); // 創建了一個對象// 畫圓: 顏色:red x:10 y:10 radius:10// 畫圓: 顏色:red x:1 y:1 radius:100// 畫圓: 顏色:red x:5 y:5 radius:50// 創建了一個對象// 畫圓: 顏色:blue x:1 y:1 radius:50// 畫圓: 顏色:blue x:12 y:12 radius:50// 畫圓: 顏色:blue x:2 y:12 radius:20
我們通過一個工廠來動態創建Circle對象,將實例進行保存,保存的位置稱之為享元池。第二次創建時,直接使用已有的結果。節約了系統資源
享元模式總結:
優點:* 大大減少對象的創建,降低系統內存使用,使效率提高。* 享元模式外部狀態獨立,不會影響其內部狀態,使得享元對象可以在不同環境被共享。
缺點:* 提高了系統復雜度,且需要相同的屬性,否則會造成系統混亂
適用場景:* 一個系統有大量相同或相似的對象,造成內存大量耗費。* 對象大部分狀態都可以外部化* 在使用享元模式時需要維護一個存儲享元對象的享元池,而這需要耗費一定的系統資源。因此使用時要衡量。
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《javascript面向對象入門教程》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結》
希望本文所述對大家JavaScript程序設計有所幫助。
相關文章: