设计模式
创建型模式
单例模式
单例模式的八种方式
-
饿汗式(静态常量)
-
饿汉式(静态代码块)
-
懒汉式(线程不安全)
-
懒汉式(线程安全,同步方法)
-
懒汉式(线程不安全,同步代码块)
-
双重检查
-
静态内部类
-
枚举
饿汉式(静态常量)
public class SingletonTest1 {
public static void main(String[] args) {
// 测试
for (int i = 0; i < 10; i++) {
new Thread(() -> {
Singleton singleton = Singleton.getInstence();
System.out.println("饿汗式(静态变量):" + Thread.currentThread().getName() + ":" + singleton.hashCode());
}).start();
}
}
}
// 饿汗式(静态变量)
class Singleton {
// 1.构造器私有化,外部不能new
private Singleton() {
}
// 2.本类内部创建对象实例
private final static Singleton instence = new Singleton();
// 3.提供一个公有的静态方法,返回实例对象
public static Singleton getInstence() {
return instence;
}
}
饿汉式(静态代码块)
public class SingletonTest02 {
public static void main(String[] args) {
Singleton02 singleton1 = Singleton02.getInstence();
Singleton02 singleton2 = Singleton02.getInstence();
System.out.println(singleton1 == singleton2); // true
System.out.println(singleton1.hashCode());
System.out.println(singleton2.hashCode());
}
}
// 饿汗式(静态代码块)
class Singleton02{
// 1.构造器私有化,外部不能new
private Singleton02(){
}
// 2.本类内部创建对象实例
private static Singleton02 instence;
// 在静态代码块执行时,创建单例对象
static {
instence = new Singleton02();
}
public static Singleton02 getInstence(){
return instence;
}
}
懒汉式(线程不安全)
public class SingletonTest03 {
public static void main(String[] args) {
for (int i = 0; i < 15; i++) {
new Thread(()->{
Singleton03 singleton1 = Singleton03.getInstance();
System.out.println(Thread.currentThread().getName()+":"+singleton1.hashCode());
}).start();
}
}
}
// 懒汉式(线程不安全)
class Singleton03{
private static Singleton03 singleton;
private Singleton03(){
}
public static Singleton03 getInstance() {
if (null == singleton) {
singleton = new Singleton03();
}
return singleton;
}
}
懒汉式(线程安全,同步方法)
public class SingletonTest04 {
public static void main(String[] args) {
for (int i = 0; i < 15; i++) {
new Thread(()->{
Singleton04 singleton1 = Singleton04.getInstance();
System.out.println(Thread.currentThread().getName()+":"+singleton1.hashCode());
}).start();
}
}
}
// 懒汉式(线程安全,同步方法)
class Singleton04{
private static Singleton04 singleton;
private Singleton04(){
}
public static synchronized Singleton04 getInstance() {
if (null == singleton) {
singleton = new Singleton04();
}
return singleton;
}
}
懒汉式(线程不安全,同步代码块)
public class SingletonTest05 {
public static void main(String[] args) {
for (int i = 0; i < 15; i++) {
new Thread(() -> {
Singleton05 singleton1 = Singleton05.getInstance();
System.out.println(Thread.currentThread().getName() + ":" + singleton1.hashCode());
}).start();
}
}
}
// 懒汉式(线程不安全,同步代码块)
class Singleton05 {
private static Singleton05 singleton;
private Singleton05() {
}
public static Singleton05 getInstance() {
if (null == singleton) {
synchronized (Singleton05.class) {
singleton = new Singleton05();
}
}
return singleton;
}
}
双重检查
public class SingletonTest06 {
public static void main(String[] args) {
for (int i = 0; i < 15; i++) {
new Thread(() -> {
Singleton06 singleton1 = Singleton06.getInstance();
System.out.println(Thread.currentThread().getName() + ":" + singleton1.hashCode());
}).start();
}
}
}
// 双重检查
class Singleton06 {
private static Singleton06 singleton;
private Singleton06() {
}
public static Singleton06 getInstance() {
if (singleton == null) {
synchronized (Singleton06.class) {
if(singleton == null) {
singleton = new Singleton06();
}
}
}
return singleton;
}
}
静态内部类
public class SingletonTest07 {
public static void main(String[] args) {
for (int i = 0; i < 15; i++) {
new Thread(() -> {
Singleton07 singleton1 = Singleton07.getInstance();
System.out.println("静态内部类:"+Thread.currentThread().getName() + ":" + singleton1.hashCode());
}).start();
}
}
}
// 静态内部类
class Singleton07 {
private static class SingletonInstance{
private static final Singleton07 INSTANCE = new Singleton07();
}
private Singleton07() {
}
public static Singleton07 getInstance() {
return SingletonInstance.INSTANCE;
}
}
枚举
public class SingletonTest08 {
public static void main(String[] args) {
for (int i = 0; i < 15; i++) {
new Thread(() -> {
Singleton08 instance = Singleton08.INSTANCE;
instance.sayHello();
System.out.println("枚类:"+Thread.currentThread().getName() + ":" + instance.hashCode());
}).start();
}
}
}
// 枚举,可以实现单例
enum Singleton08{
INSTANCE;
public void sayHello() {
System.out.println("hello...");
}
}
单例模式注意事项和细节说明
- 单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要繁琐创建销毁的对象,使用单例模式可以提供系统性能
- 当想实例化一个单例类的时候,必须使用相应的获取对象的方法,而不是使用new
- 构造函数是私有的