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 b56368f

Browse files
author
deeper
committed
添加单例模式
1 parent 238a2af commit b56368f

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.ruoxu.pattern.singleton;
2+
3+
public class Demo {
4+
public static void main(String[] args) {
5+
Singleton1 instance11 = Singleton1.getInstance();
6+
Singleton1 instance12 = Singleton1.getInstance();
7+
8+
Singleton2 instance21 = Singleton2.getInstance();
9+
Singleton2 instance22 = Singleton2.getInstance();
10+
11+
Singleton3 instance31 = Singleton3.getInstance();
12+
Singleton3 instance32 = Singleton3.getInstance();
13+
14+
Singleton4 instance41 = Singleton4.getInstance();
15+
Singleton4 instance42 = Singleton4.getInstance();
16+
17+
System.out.println(instance11==instance12);
18+
System.out.println(instance21==instance22);
19+
System.out.println(instance31==instance32);
20+
System.out.println(instance41==instance42);
21+
22+
23+
}
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ruoxu.pattern.singleton;
2+
3+
public class Singleton1 {
4+
// 饿汉模式
5+
private static final Singleton1 singleton = new Singleton1();
6+
7+
public static Singleton1 getInstance(){
8+
return singleton;
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ruoxu.pattern.singleton;
2+
3+
public class Singleton2 {
4+
// 懒汉模式
5+
private static Singleton2 instance;
6+
7+
public static synchronized Singleton2 getInstance(){//synchronized在此处导致速度略慢
8+
if(instance == null){
9+
instance = new Singleton2();
10+
}
11+
return instance;
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.ruoxu.pattern.singleton;
2+
3+
public class Singleton3 {
4+
// Double Check Lock(DCL) 推荐1
5+
private static Singleton3 instance = null;
6+
7+
public static Singleton3 getInstance(){
8+
if(instance == null){
9+
synchronized (Singleton3.class) {
10+
if(instance==null){
11+
instance = new Singleton3();
12+
}
13+
}
14+
}
15+
return instance;
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ruoxu.pattern.singleton;
2+
3+
public class Singleton4 {
4+
// 静态内部类 推荐2
5+
public static Singleton4 getInstance(){
6+
return Holder.instance;
7+
}
8+
9+
private static class Holder{
10+
private static final Singleton4 instance = new Singleton4();
11+
}
12+
13+
}

0 commit comments

Comments
(0)

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