这个定义可能看起来比较拗口,实际上我们再日常中可能不知不觉就用到了享元模式的思想,我们可以把享元模式类比为现在的共享经济,例如某个共享单车存放点有 n 辆单车,那么当用户需要时,我们就将单车借出去,用户用完了,就把单车还回来,如果用户来借单车时没有单车了,就从仓库拿出新单车,这样的话,就能通过少量单车数量满足大量用户使用了。
const a = new People("aaa"); const b = new People("bbb"); const c = new People("ccc");
a.borrow(); // "aaa borrowed the NO.1 bike." a.use(); //"The bike 1 is using!" b.borrow(); //"bbb borrowed the NO.2 bike." b.use(); //"The bike 2 is using!" a.re(); c.borrow(); // "ccc borrowed the NO.1 bike." c.use(); //"The bike 1 is using!"