单例模式和策略模式

一个类返回一个实例,再次创建时调用直接返回

class CreateUser{

     constructor(name){

          this.name = name;

         this.getName();

    }

    getName(){

         return this.name;

   }

}

const ProxyModel = ( () =>{

       let instance = null;  

       return (name) => {

           if(!instance){

               instance = new CreateUser(name);      

          }

          return instance;

      }

} )();

let p1 = new ProxyModel("vn");

let p2 = new ProxyModel("ln");

console.info(p1,p2);

策略类只关注算法的实现,定义统一的接口来调用这些方法

const levelData = {

      "a" : money => money * 4,

      "b" : money => money * 3,

      "c" : money => money * 2

}

const getMoney = (level,money) => levelData[level](money);

console.info(getMoney("a",200));

原文地址:https://www.cnblogs.com/wangc04/p/12107398.html