单例模式
一个类只能有一个实例。比如打印机这种东西。
trick在于:变量,构造方法都必须为private,所以其他类new它的构造方法是不行的,直接调用变量也不行。只能调用getInstance()方法&其他被public修饰的方法。getInstance()方法记得用public修饰。
原理:
public class Singleton {
private static Singleton uniqueInstance = null;
private Singleton(){
}
public static Singleton getInstance(){
if(uniqueInstance == null){
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}
例子:
public class ChocolateFactory {
private boolean empty;//是否为空
private boolean boiled;//是否加热过了
private static ChocolateFactory uniqueInstance = null;
private ChocolateFactory(){
empty = true;
boiled = false;
}
public static ChocolateFactory getInstance(){
if(uniqueInstance == null){
uniqueInstance = new ChocolateFactory();
}
return uniqueInstance;
}
public void fill(){
if(empty && (!boiled)){
empty = false;
}
}
public void boil(){
if((!empty) && (!boiled)){
boiled = true;
}
}
public void pour(){
if((!empty) && boiled){
empty = true;
}
}
}
bug:多线程不安全。
解决办法(优化):
vs1:放一个同步锁(一个线程执行完后,另一个线程才可以调用),但是比较耗资源。
public class ChocolateFactory {
private boolean empty;
private boolean boiled;
private static ChocolateFactory uniqueInstance = null;
private ChocolateFactory(){
empty = true;
boiled = false;
}
//1、加入一个synchronized关键字
public static synchronized ChocolateFactory getInstance(){
if(uniqueInstance == null){
uniqueInstance = new ChocolateFactory();
}
return uniqueInstance;
}
public void fill(){
if(empty && (!boiled)){
empty = false;
}
}
public void boil(){
if((!empty) && (!boiled)){
boiled = true;
}
}
public void pour(){
if((!empty) && boiled){
empty = true;
}
}
}
vs2:急切创建实例,但是也有点耗资源。
public class ChocolateFactory {
private boolean empty;
private boolean boiled;
//1、快速创建一个实例
private static ChocolateFactory uniqueInstance = new ChocolateFactory();
private ChocolateFactory(){
empty = true;
boiled = false;
}
public static ChocolateFactory getInstance(){
if(uniqueInstance == null){
uniqueInstance = new ChocolateFactory();
}
return uniqueInstance;
}
public void fill(){
if(empty && (!boiled)){
empty = false;
}
}
public void boil(){
if((!empty) && (!boiled)){
boiled = true;
}
}
public void pour(){
if((!empty) && boiled){
empty = true;
}
}
}
public class ChocolateFactory {
private boolean empty;
private boolean boiled;
//1、volatile是为了处理多线程安全而加的关键字
private volatile static ChocolateFactory uniqueInstance = null;
private ChocolateFactory(){
empty = true;
boiled = false;
}
public static ChocolateFactory getInstance(){
if(uniqueInstance == null){
//2、增多了一个锁
synchronized(ChocolateFactory.class){
if(uniqueInstance == null){
uniqueInstance = new ChocolateFactory();
}
}
}
return uniqueInstance;
}
public void fill(){
if(empty && (!boiled)){
empty = false;
}
}
public void boil(){
if((!empty) && (!boiled)){
boiled = true;
}
}
public void pour(){
if((!empty) && boiled){
empty = true;
}
}
}
有一个方法,适用于看代码是否应该使用当前这种设计模式:
要在原来代码基础上,多加一个什么功能或什么改动时,改动会复杂吗?如果会变复杂,则考虑采用其他的设计模式。
要在原来代码基础上,多加一个什么功能或什么改动时,改动会复杂吗?如果会变复杂,则考虑采用其他的设计模式。