Skip to content

Commit 92bd0eb

Browse files
author
liuyj-m
committed
分享
1 parent 1c02f5b commit 92bd0eb

47 files changed

Lines changed: 274 additions & 163 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
3.96 KB
Binary file not shown.
4 KB
Binary file not shown.

src/a01_FactoryMethod/simpleFactory/Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class Test {
88

99
public static void main(String[] args) {
1010
CarFactory factory = new CarFactory();
11-
Car sender = factory.produce("audi");
12-
sender.run();
11+
Car car = factory.produce("byd");
12+
car.run();
1313
}
1414
}
3.51 KB
Binary file not shown.
3.68 KB
Binary file not shown.

src/a03_Singleton/Singleton.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
public class Singleton {
88

9-
/* 持有私有静态实例,防止被引用,此处赋值为null,目的是实现延迟加载 */
9+
/* 持有私有静态实例,防止被引用*/
1010
private static Singleton instance = new Singleton();
1111

1212
/* 私有构造方法,防止被实例化 */
@@ -17,5 +17,5 @@ private Singleton() {
1717
public static Singleton getInstance() {
1818
return instance;
1919
}
20-
20+
2121
}

src/a03_Singleton/Singleton2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ private Singleton2() {
1515

1616
/* 静态工程方法,创建实例 */
1717
public synchronized static Singleton2 getInstance() {
18-
if (instance == null) {
18+
if (instance == null) {
1919
instance = new Singleton2();
2020
}
21-
return instance;
21+
return instance;
2222
}
2323

2424
}

src/a03_Singleton/Singleton3.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
/**
44
* 单例模式 - 双重检查
5-
*
5+
*从1.5开始,加了volatile关键字的引用,它的初始化就不能是:
6+
- a. 先分配内存,让instance指向这块内存
7+
- b. 在内存中创建对象
8+
9+
而应该是:
10+
- a.在内存中创建对象
11+
- b.让instance指向这个对象.
612
*/
713
public class Singleton3 {
814

@@ -17,11 +23,11 @@ private Singleton3() {
1723
public static Singleton3 getInstance() {
1824
//double check
1925
if (instance == null) {
20-
synchronized (instance) {
21-
if (instance == null) {
22-
instance = new Singleton3();
26+
synchronized (Singleton3.class) {
27+
if (instance == null) {
28+
instance = new Singleton3();
2329
}
24-
}
30+
}
2531
}
2632
return instance;
2733
}

src/a04_Builder/AudiCar.java

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/a04_Builder/BydCar.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)