Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8d171dc

Browse files
committed
🎉 singleton pattern
1 parent b24ac8e commit 8d171dc

File tree

7 files changed

+245
-0
lines changed

7 files changed

+245
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pers.huangyuhui.singleton_pattern.IoDH_singleton;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 客户端测试类
6+
* @author: 黄宇辉
7+
* @date: 9/6/2019-11:14 AM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class Client {
12+
13+
public static void main(String[] args) {
14+
System.out.println(Singleton.getInstance() == Singleton.getInstance()); //ture
15+
}
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package pers.huangyuhui.singleton_pattern.IoDH_singleton;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 单例模式-内部类实现单例类
6+
* @author: 黄宇辉
7+
* @date: 9/6/2019-11:11 AM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class Singleton {
12+
private Singleton() {
13+
14+
}
15+
16+
//静态内部类
17+
public static class HolderClass {
18+
private final static Singleton instance = new Singleton();
19+
}
20+
21+
public static Singleton getInstance() {
22+
return HolderClass.instance;
23+
}
24+
25+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
---
2+
title: Java设计模式之Singleton Pattern
3+
date: 2019年09月06日 13:32:05
4+
tags: [Java,design and pattern]
5+
---
6+
7+
## 学习笔记 : Java设计模式之Singleton Pattern
8+
9+
### 概念
10+
*单例设计模式 : 确保一个类只能有一个实例,并提供一个全局访问点来访问这个实例. (Singleton Pattern : Ensure a class has only one intance,and provide a global point of access to it. ). 单例设计模式是一种对象创建型模式,其主要有三个要点 :*
11+
1. *某个类只能有一个实例*
12+
2. *必须自行创建这个实例*
13+
3. *必须自行向系统提供这个实例*
14+
15+
16+
### 示例程序-饿汉式单例
17+
*饿汉式单例类( Eager Singleton )是最简单的单例类,既类被加载时静态变量 instance 就会被初始化,程序示例如下所示 :*
18+
```java
19+
package pers.huangyuhui.singleton_pattern.eager_singleton;
20+
21+
/**
22+
* @project: design-patterns
23+
* @description: 单例模式-饿汉式单例类
24+
* @author: 黄宇辉
25+
* @date: 9/6/2019-10:44 AM
26+
* @version: 1.0
27+
* @website: https://yubuntu0109.github.io/
28+
*/
29+
public class EagerSingleton {
30+
31+
private static final EagerSingleton instance = new EagerSingleton();
32+
33+
private EagerSingleton() {
34+
35+
}
36+
37+
public static EagerSingleton getInstance() {
38+
return instance;
39+
}
40+
}
41+
```
42+
43+
*注 : 从资源利用效率的角度来讲饿汉式单例不及懒汉式单例,而且系统加载时由于需要创建饿汉式单例对象,加载时间可能比较长.*
44+
45+
46+
### 程序示例-懒汉式单例
47+
*与饿汉式单例不同的是,懒汉式单例类( Lazy Singleton )在第一次被引用时才会将自己实例化,当单例类被加载时并不会将自己实例化, 示例程序如下所示 :*
48+
```java
49+
package pers.huangyuhui.singleton_pattern.lazy_singleton;
50+
51+
/**
52+
* @project: design-patterns
53+
* @description: 单例模式-懒汉式单例类
54+
* @author: 黄宇辉
55+
* @date: 9/6/2019-10:50 AM
56+
* @version: 1.0
57+
* @website: https://yubuntu0109.github.io/
58+
*/
59+
public class LazySingleton {
60+
61+
//被volatile修饰的成员变量可以确保多个线程都能够正确处理
62+
private volatile static LazySingleton instance = null;
63+
64+
private LazySingleton() {
65+
66+
}
67+
68+
public static LazySingleton getInstance() {
69+
//第一重判断
70+
if (instance == null) {
71+
//锁定代码块:确保任何时刻只有一个线程可执行此代码块
72+
synchronized (LazySingleton.class) {
73+
//第二重判断:防止产生多个单例对象
74+
if (instance == null) {
75+
instance = new LazySingleton();
76+
}
77+
}
78+
}
79+
return instance;
80+
}
81+
}
82+
```
83+
84+
*注 : 由于`volatile`关键字会屏蔽Java虚拟机所做的一切代码优化,继而可能会导致系统的运行效率降低,因此即使使用双重检查锁定来实现单例模式也不是一种完美的实现方法哟~*
85+
86+
87+
### 示例程序-内部类实现单例
88+
*饿汉式单例不能实现延迟加载,不管将来用不用始终占据内存. 懒汉式单例类线程安全控制繁琐,而且性能受影响. 为了克服这些问题,在Java语言中可以通过使用 Initialization on Demand Holder( `IoDH` )技术来实现单例模式,示例程序如下所示 :*
89+
```java
90+
package pers.huangyuhui.singleton_pattern.IoDH_singleton;
91+
92+
/**
93+
* @project: design-patterns
94+
* @description: 单例模式-内部类实现单例类
95+
* @author: 黄宇辉
96+
* @date: 9/6/2019-11:11 AM
97+
* @version: 1.0
98+
* @website: https://yubuntu0109.github.io/
99+
*/
100+
public class Singleton {
101+
private Singleton() {
102+
103+
}
104+
105+
//静态内部类
106+
public static class HolderClass {
107+
private final static Singleton instance = new Singleton();
108+
}
109+
110+
public static Singleton getInstance() {
111+
return HolderClass.instance;
112+
}
113+
114+
}
115+
```
116+
117+
*注 : 通过使用`IoDH`既可以实现延迟加载,又可以保证线程安全,不影响系统性能,既其为一种最好的Java语言单例模式实现方式,其缺点为很多面向对象语言并不支持IoDH哟~*
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pers.huangyuhui.singleton_pattern.eager_singleton;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 客户端测试类
6+
* @author: 黄宇辉
7+
* @date: 9/6/2019-10:47 AM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class Client {
12+
13+
public static void main(String[] args) {
14+
System.out.println(EagerSingleton.getInstance() == EagerSingleton.getInstance()); //true
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pers.huangyuhui.singleton_pattern.eager_singleton;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 单例模式-饿汉式单例类
6+
* @author: 黄宇辉
7+
* @date: 9/6/2019-10:44 AM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class EagerSingleton {
12+
13+
private static final EagerSingleton instance = new EagerSingleton();
14+
15+
private EagerSingleton() {
16+
17+
}
18+
19+
public static EagerSingleton getInstance() {
20+
return instance;
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package pers.huangyuhui.singleton_pattern.lazy_singleton;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 客户端测试类
6+
* @author: 黄宇辉
7+
* @date: 9/6/2019-10:52 AM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class Client {
12+
13+
public static void main(String[] args) {
14+
System.out.println(LazySingleton.getInstance() == LazySingleton.getInstance()); //true
15+
}
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package pers.huangyuhui.singleton_pattern.lazy_singleton;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 单例模式-懒汉式单例类
6+
* @author: 黄宇辉
7+
* @date: 9/6/2019-10:50 AM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class LazySingleton {
12+
13+
//被volatile修饰的成员变量可以确保多个线程都能够正确处理
14+
private volatile static LazySingleton instance = null;
15+
16+
private LazySingleton() {
17+
18+
}
19+
20+
public static LazySingleton getInstance() {
21+
//第一重判断
22+
if (instance == null) {
23+
//锁定代码块:确保任何时刻只有一个线程可执行此代码块
24+
synchronized (LazySingleton.class) {
25+
//第二重判断:防止产生多个单例对象
26+
if (instance == null) {
27+
instance = new LazySingleton();
28+
}
29+
}
30+
}
31+
return instance;
32+
}
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /