• 单例模式


    一、单例模式

    // 单例模式
    class SingleMode {
      static getInstance(name, age) {
        if (!SingleMode.instance) {
          SingleMode.instance = new SingleMode(name, age);
        }
        return SingleMode.instance;
      }
    
      constructor(name, age) {
        this.name = name;
        this.age = age;
      }
    
      setInfo(name, age) {
        this.name = name;
        this.age = age;
        console.log(this.name, this.age);
      }
    }
    
    const s1 = SingleMode.getInstance('shangyueyue', 18);
    const s2 = SingleMode.getInstance('shangyueyue', 18);
    console.log(s1, s2, s1 === s2);
    // SingleMode { name: 'shangyueyue', age: 18 } SingleMode { name: 'shangyueyue', age: 18 } true
    s2.setInfo('huyating', 23);
    console.log(s1, s2, s1 === s2);
    // SingleMode { name: 'huyating', age: 23 } SingleMode { name: 'huyating', age: 23 } true
    
    // 单例模式
    class SingleMode2 {
      constructor(name, age) {
        if (SingleMode2.instance) return SingleMode2.instance;
        this.name = name;
        this.age = age;
        SingleMode2.instance = this;
        return this;
      }
    
      setInfo(name, age) {
        this.name = name;
        this.age = age;
        console.log(this.name, this.age);
      }
    }
    const s3 = new SingleMode2('shangyueyue', 18);
    const s4 = new SingleMode2('shangyueyue', 18);
    console.log(s3, s4, s3 === s4);
    // SingleMode { name: 'shangyueyue', age: 18 } SingleMode { name: 'shangyueyue', age: 18 } true
    s3.setInfo('huyating', 23);
    console.log(s3, s4, s3 === s4);
    // SingleMode { name: 'huyating', age: 23 } SingleMode { name: 'huyating', age: 23 } true
  • 相关阅读:
    浅析WPhone、Android的Back与Home键
    Android音频播放之SoundPool
    Button、ImageButton及ImageView详解
    文本 To 音频
    gravity、layout_gravity及orientation
    项目规范性检测工具Lint
    Android视频播放之VideoView
    ContentProvider数据访问详解
    QQ第三方登录
    Android数据共享
  • 原文地址:https://www.cnblogs.com/shangyueyue/p/10824285.html
Copyright © 2020-2023  润新知