常用的设计模式总结(持续更新。。。)

290 阅读1分钟

单例模式

class Singleton {
	constructor(name) {
    	this.name = name;
        this.instance = null;
    }
    
    static getInstance(name) {
    	if (!this.instance) {
        	this.instance = new Singleton(name);
        }
        
        return this.instance;
    }
}

const oA = Singleton.getInstance("hello");
const oB = Singleton.getInstance("world");

console.log(oA === oB);

观察者模式

发布订阅模式