homework: 单例模式
class Demo {private static instance: Demo;private constructor(public name: string) {};static getInstance(name: string): Demo {if (!this.instance) {this.instance = new Demo(name);}return this.instance;}}const d1 = Demo.getInstance('Jack');const d2 = Demo.getInstance('Lily');console.log(d1.name);console.log(d2.name);// Jack// Jack
homework: 接口重载
Flyweight Pattern(享元模式):主要用于减少创建对象的数量,以减少内存占用和提高性能。
type OrderID = string & { readonly brand: unique symbol };type UserID = string & { readonly brand: unique symbol };type ID = OrderID | UserID;// (伴侣模式): 一种非正式编程约束function OrderID (id: string) {return id as OrderID;}function UserID (id: string) {return id as UserID;}function queryForUser(id: ID) {}queryForUser('ll');// Argument of type 'string' is not assignable to parameter of type 'ID'.ts(2345)queryForUser(UserID('ll'));
