几种常用设计模式

  |   0浏览

//工厂模式

function newObject(color,age,...arg){let obj= new Object()obj.color= color;obj.age = age;...arg;return obj;}

//单例模式function getinstance=(function(){let object;return function(){if(object ==undefined){obj = new doSome()return object;}}})();使用let instance = getinstance()

//观察者模式

let mypromise = function(){return new Promise((resolve,reject)=>{if(true){resolve(dosometing)}else{reject('error')}

 }) 

}//使用mypromise.then(res=>{console.log(res)})

//策略模式let object = {"fist":function(){dosmething;},"second":function(){dosmething;},"thired":function(){doSomething;}}//代理模式

function isSend(message){console.log(message+"目标对象")}function proxyObject(message){let message = dosometing(message);isSend(message)}

原文地址:https://blog.51cto.com/14582569/2507127