Singleton model and strategy mode

Directly call returns an instance of a class returns, create again

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);

Strategy only concerned with implementation of the algorithm, we define a unified interface to call these methods

levelData const = {

      "a" : money => money * 4,

      "b" : money => money * 3,

      "c" : money => money * 2

}

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

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

Guess you like

Origin www.cnblogs.com/wangc04/p/12107398.html