javascript設計模式-策略模式

來源:互聯網
上載者:User

標籤:替換   name   表單   puts   src   input   null   val   test   

策略模式筆記
   將定義的一組演算法封裝起來,使其相互之間可以替換。
   封裝的演算法具有一定獨立性,不會隨用戶端變化而變化。
   與狀態模式異同?
     1. 結構上看,它與狀態模式很像,也是在內部封裝一個對象,然後通過返回的介面對象實現對內部對象的調用
     2. 不同點是,策略模式不需要管理狀態,狀態之間沒有依賴關係、策略之間可以相互替換、策略對象內部儲存的是相互獨立的一些演算法。
   demo:價格策略對象 、 表單正則驗證策略對象

demo1:價格策略對象

 1             //價格策略對象 2             var PriceStrategy = function(){ 3                 //內部演算法對象 4                 var strategy = { 5                     //100 返 30 6                     return30 : function(price){ 7                         return +price + parseInt(price / 100) * 30; 8                     }, 9                     //100 返 5010                     return50 : function(price){11                         return +price + parseInt(price / 100) * 50;12                     },13                     //9折14                     percent90 : function(price){15                         return price * 100 * 90 / 10000;16                     },17                     //8折18                     percent80 : function(price){19                         return price * 100 * 80 / 10000;20                     },21                     //5折22                     percent50 : function(price){23                         return price * 100 * 50 / 10000;24                     }25                 }26                 return function(algorithm,price){27                     //如果演算法存在,則調用演算法,否則返回false28                     return strategy[algorithm] && strategy[algorithm](price);29                 }30             }();

測試代碼

1             var price = PriceStrategy(‘return50‘,‘300‘);2             console.log(price);

控制台顯示

demo2: 表單正則驗證策略對象

 1             //表單正則驗證策略對象 2             var InputStrategy = function(){ 3                 var strategy = { 4                     //是否為空白 5                     notNull : function(value){ 6                         return /\s+/.test(value) ? ‘請輸入內容‘ : ‘‘; 7                     }, 8                     //是否是一個數字 9                     number : function(value){10                         return /^[0-9]+(\.[0-9]+)?$/.test(value) ? ‘‘ : ‘請輸入數字‘;11                     },12                     //是否是本地電話13                     phone : function(value){14                         return /^\d{3}\-\d{8}$|^\d{4}\-\d{7}$/.test(value) ? ‘‘ : ‘請輸入正確的電話號碼格式,如:010-12345678 或 0418-1234567‘;15                     }16                 }17                 return {18                     //驗證介面 type演算法 value 表單值19                     check : function(type, value){20                         //去除收尾空白符21                         value =value.replace(/^\s+|\s+$/g, ‘‘);22                         return strategy[type] ? strategy[type](value) : ‘沒有該類型的檢測方法‘23                     },24                     //添加策略25                     addStrategy : function(type, fn){26                         strategy[type] = fn;27                     }28                 }29             }();

拓展策略 添加策略

1          InputStrategy.addStrategy(‘nickname‘, function(value){2              return /^[a-zA-Z]\w{3-7}$/.test(value) ? ‘‘ : ‘請輸入4-8位暱稱,如:YYQH‘;3          });

測試代碼

1          console.log(InputStrategy.check(‘nickname‘,"1YQH"));

控制台顯示

javascript設計模式-策略模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.