饿汉式:
package com.witwicky.domain;
/**
* 饿汉式
*/
public class Person2 {
public static final Person2 person = new Person2();
private Person2() {
}
public static Person2 getInstance() {
return person;
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
懒汉式:
1.
package com.witwicky.domain;
/**
* 懒汉式1
*/
public class Person3 {
public static Person3 person = null;
private Person3() {
}
public static Person3 getInstance() {
if (null == person) {
person = new Person3();
return person;
}
return person;
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2.
package com.witwicky.domain;
/**
* 懒汉式2
*/
public class Person4 {
public static Person4 person = null;
private Person4() {
}
/**
* 使用同步锁,锁定当前线程所执行的方法
* @return
*/
public static synchronized Person4 getInstance() {
if (null == person) {
person = new Person4();
return person;
}
return person;
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.
package com.witwicky.domain;
/**
* 懒汉式3
*/
public class Person5 {
public static Person5 person = null;
private Person5() {
}
/**
* 使用同步锁,锁定当前线程所执行的方法中的具体代码
* @return
*/
public static Person5 getInstance() {
if (null == person) {
synchronized (Person5.class) {
person = new Person5();
};
return person;
}
return person;
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
双重检测式:
package com.witwicky.domain;
/**
* 双重检测
*/
public class Person6 {
public static Person6 person = null;
private Person6() {
}
/**
* 使用同步锁,锁定当前线程所执行的方法中的具体代码
* @return
*/
public static Person6 getInstance() {
if (null == person) {
synchronized (Person6.class) {
if(null == person) {
person = new Person6();
}
};
return person;
}
return person;
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
===
测试Main方法.java:
package com.witwicky.test;
import com.witwicky.domain.*;
public class TestSingleton {
public static void main(String[] args) {
// Person p1 = new Person();
// p1.setName("bob");
// p1.setAge(23);
//
// Person p2 = new Person();
// p2.setName("jack");
// p2.setAge(32);
// Person2 p1 = Person2.getInstance();
// p1.setName("bob");
// p1.setAge(23);
//
// Person2 p2 = Person2.getInstance();
// p2.setName("jack");
// p2.setAge(32);
// Person3 p1 = Person3.getInstance();
// p1.setName("bob");
// p1.setAge(23);
//
// Person3 p2 = Person3.getInstance();
// p2.setName("jack");
// p2.setAge(32);
// Person4 p1 = Person4.getInstance();
// p1.setName("bob");
// p1.setAge(23);
//
// Person4 p2 = Person4.getInstance();
// p2.setName("jack");
// p2.setAge(32);
// Person5 p1 = Person5.getInstance();
// p1.setName("bob");
// p1.setAge(23);
//
// Person5 p2 = Person5.getInstance();
// p2.setName("jack");
// p2.setAge(32);
Person6 p1 = Person6.getInstance();
p1.setName("bob");
p1.setAge(23);
Person6 p2 = Person6.getInstance();
p2.setName("jack");
p2.setAge(32);
System.out.println(p1);
System.out.println(p2);
}
}