这个类只能实例化出一个对象
- 单例模式 – 主席类案例
- 通过一个类 只能实例化唯一的一个对象
- 私有化
- 默认构造
- 拷贝构造
- 唯一实例指针
- 对外提供 getInstance 接口,将指针返回
- 单例模式 – 打印机案例
- 和主席类案例一样设计单例模式
提供打印功能并且统计打印次数
- 不能实例化对象,将构造函数私有化,不可以创建对象
- 需要提供一个类(静态),所有变量对应的是一个东西,将指针 私有化,对外提供只读接口,唯一实例指针
- 不能克隆类,也就是不能拷贝构造
单例模式-主席类案例
单例模式 - 打印机案例