zoukankan      html  css  js  c++  java
  • 单例模式和策略模式

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

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

  • 相关阅读:
    Xshell 设置右键粘贴功能
    python中dict操作集合
    mac 设置网页字体
    博客收藏
    memcache 安装与简单使用
    mac安装homebrew
    Graphviz下载 使用
    jekyll 与hexo
    js 汉字排序
    初试gem
  • 原文地址:https://www.cnblogs.com/wangc04/p/12107398.html
Copyright © 2011-2022 走看看