懒汉式:
public class Test6 {
static class Animal {
private static Animal instance = null;
private String name;
private String color;
private int height;
private Animal(String name, String color, int height) {
super();
this.name = name;
this.color = color;
this.height = height;
}
//构造函数私有,实例化对象只在本类范围内
private Animal() {
}
//拿到单例的方法静态
public static Animal GetInstance() {
//只有实例为空时,即需要创建一个实例时,才需加锁创建
//若已存在一个实例,就直接返回该实例。
if (instance == null) {
//无法保证线程安全。当有多个线程同时访问getInstance的时候,此时若对象为空,
//就会出现会多个线程同时产生多个Singleton对象,故加锁。
synchronized (Animal.class) {
if (instance == null) {
//在方法中对声明的对象进行实例化,在判断条件成立的前提下,只要类一加载
//对象不会实例化,必须调用获得对象的方法才能实例化此对象,这就是延迟加载。
instance = new Animal();
}
}
}
return instance;
}
@Override
public String toString() {
return "名字:" + name + " 颜色: " + color + " 身高:" + height;
}
}
public static void main(String[] args) throws Exception {
Animal a1 = Animal.GetInstance();
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
throw new Exception("出错啦...");
}
Animal a2 = Animal.GetInstance();
System.out.println(a1 == a2);
a1 = new Animal("长颈鹿", "黄色", 210);
a2 = new Animal("熊猫", "黑色", 140);
System.out.println(a1 == a2);
System.out.println(a1);
System.out.println(a2);
}
}
饿汉式:public class Test7 {
static class People {
private static People instance = new People();
private String name;
private String sex;
private int age;
private People(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
//构造函数私有,实例化对象只在本类范围内
private Animal() {
}
//拿到单例的方法静态
public static People GetInstance() {
return instance;
}
@Override
public String toString() {
return "姓名:" + name + " 性别: " + sex + " 年龄:" + age;
}
}
public static void main(String[] args) throws Exception {
People a1 = People.GetInstance();
try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
throw new Exception("出错啦...");
}
People a2 = People.GetInstance();
System.out.println(a1 == a2);
a1 = new People("张飞", "男", 23);
a2 = new People("貂蝉", "女", 21);
System.out.println(a1 == a2);
System.out.println(a1);
System.out.println(a2);
}
}
登记式:
import java.util.HashMap;
import java.util.Map;
//登记式单例类.
//类似Spring里面的方法,将类名注册,下次从里面直接获取。
public class Singleton3 {
private static Map<String,Singleton3> map = new HashMap<String,Singleton3>();
static{
Singleton3 single = new Singleton3();
map.put(single.getClass().getName(), single);
}
//保护的默认构造子
protected Singleton3(){}
//静态工厂方法,返还此类惟一的实例
public static Singleton3 getInstance(String name) {
if(name == null) {
name = Singleton3.class.getName();
System.out.println("name == null"+"--->name="+name);
}
if(map.get(name) == null) {
try {
map.put(name, (Singleton3) Class.forName(name).newInstance());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return map.get(name);
}
//一个示意性的商业方法
public String about() {
return "Hello, I am RegSingleton.";
}
public static void main(String[] args) {
Singleton3 single3 = Singleton3.getInstance(null);
System.out.println(single3.about());
}
}
|