Skip to content

Commit e6c8ca7

Browse files
committed
设计模式之Singleton和Strategy
1 parent 4418487 commit e6c8ca7

File tree

21 files changed

+794
-70
lines changed

21 files changed

+794
-70
lines changed

.idea/encodings.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 386 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

src/com/mashibing/singleton/Main.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.mashibing.singleton;
2+
3+
public class Main {
4+
5+
public static void main(String[] args) {
6+
//Mgr01 mgr = Mgr01.getInstance();
7+
}
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mashibing.singleton;
2+
3+
/**
4+
* 饿汉式
5+
* 类加载到内存后,就实例化一个单例,JVM保证线程安全
6+
* 简单实用,推荐使用!
7+
* 唯一缺点:不管用到与否,类装载时就完成实例化
8+
* Class.forName("")
9+
* (话说你不用的,你装载它干啥)
10+
*/
11+
public class Mgr01 {
12+
private static final Mgr01 INSTANCE = new Mgr01();
13+
14+
private Mgr01() {};
15+
16+
public static Mgr01 getInstance() {
17+
return INSTANCE;
18+
}
19+
20+
public void m() {
21+
System.out.println("m");
22+
}
23+
24+
public static void main(String[] args) {
25+
Mgr01 m1 = Mgr01.getInstance();
26+
Mgr01 m2 = Mgr01.getInstance();
27+
System.out.println(m1 == m2);
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.mashibing.singleton;
2+
3+
/**
4+
* 跟01是一个意思
5+
*/
6+
public class Mgr02 {
7+
private static final Mgr02 INSTANCE;
8+
static {
9+
INSTANCE = new Mgr02();
10+
}
11+
12+
private Mgr02() {};
13+
14+
public static Mgr02 getInstance() {
15+
return INSTANCE;
16+
}
17+
18+
public void m() {
19+
System.out.println("m");
20+
}
21+
22+
public static void main(String[] args) {
23+
Mgr02 m1 = Mgr02.getInstance();
24+
Mgr02 m2 = Mgr02.getInstance();
25+
System.out.println(m1 == m2);
26+
}
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.mashibing.singleton;
2+
3+
/**
4+
* lazy loading
5+
* 也称懒汉式
6+
* 虽然达到了按需初始化的目的,但却带来线程不安全的问题
7+
*/
8+
public class Mgr03 {
9+
private static Mgr03 INSTANCE;
10+
11+
private Mgr03() {
12+
}
13+
14+
public static Mgr03 getInstance() {
15+
if (INSTANCE == null) {
16+
try {
17+
Thread.sleep(1);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
INSTANCE = new Mgr03();
22+
}
23+
return INSTANCE;
24+
}
25+
26+
public void m() {
27+
System.out.println("m");
28+
}
29+
30+
public static void main(String[] args) {
31+
for(int i=0; i<100; i++) {
32+
new Thread(()->
33+
System.out.println(Mgr03.getInstance().hashCode())
34+
).start();
35+
}
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.mashibing.singleton;
2+
3+
/**
4+
* lazy loading
5+
* 也称懒汉式
6+
* 虽然达到了按需初始化的目的,但却带来线程不安全的问题
7+
* 可以通过synchronized解决,但也带来效率下降
8+
*/
9+
public class Mgr04 {
10+
private static Mgr04 INSTANCE;
11+
12+
private Mgr04() {
13+
}
14+
15+
public static synchronized Mgr04 getInstance() {
16+
if (INSTANCE == null) {
17+
try {
18+
Thread.sleep(1);
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
INSTANCE = new Mgr04();
23+
}
24+
return INSTANCE;
25+
}
26+
27+
public void m() {
28+
System.out.println("m");
29+
}
30+
31+
public static void main(String[] args) {
32+
for(int i=0; i<100; i++) {
33+
new Thread(()->{
34+
System.out.println(Mgr04.getInstance().hashCode());
35+
}).start();
36+
}
37+
}
38+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.mashibing.singleton;
2+
3+
/**
4+
* lazy loading
5+
* 也称懒汉式
6+
* 虽然达到了按需初始化的目的,但却带来线程不安全的问题
7+
* 可以通过synchronized解决,但也带来效率下降
8+
*/
9+
public class Mgr05 {
10+
private static Mgr05 INSTANCE;
11+
12+
private Mgr05() {
13+
}
14+
15+
public static Mgr05 getInstance() {
16+
if (INSTANCE == null) {
17+
//妄图通过减小同步代码块的方式提高效率,然后不可行
18+
synchronized (Mgr05.class) {
19+
try {
20+
Thread.sleep(1);
21+
} catch (InterruptedException e) {
22+
e.printStackTrace();
23+
}
24+
INSTANCE = new Mgr05();
25+
}
26+
}
27+
return INSTANCE;
28+
}
29+
30+
public void m() {
31+
System.out.println("m");
32+
}
33+
34+
public static void main(String[] args) {
35+
for(int i=0; i<100; i++) {
36+
new Thread(()->{
37+
System.out.println(Mgr05.getInstance().hashCode());
38+
}).start();
39+
}
40+
}
41+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.mashibing.singleton;
2+
3+
/**
4+
* lazy loading
5+
* 也称懒汉式
6+
* 虽然达到了按需初始化的目的,但却带来线程不安全的问题
7+
* 可以通过synchronized解决,但也带来效率下降
8+
*/
9+
public class Mgr06 {
10+
private static volatile Mgr06 INSTANCE; //JIT
11+
12+
private Mgr06() {
13+
}
14+
15+
public static Mgr06 getInstance() {
16+
if (INSTANCE == null) {
17+
//双重检查
18+
synchronized (Mgr06.class) {
19+
if(INSTANCE == null) {
20+
try {
21+
Thread.sleep(1);
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
25+
INSTANCE = new Mgr06();
26+
}
27+
}
28+
}
29+
return INSTANCE;
30+
}
31+
32+
public void m() {
33+
System.out.println("m");
34+
}
35+
36+
public static void main(String[] args) {
37+
for(int i=0; i<100; i++) {
38+
new Thread(()->{
39+
System.out.println(Mgr06.getInstance().hashCode());
40+
}).start();
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)