Skip to content

Commit 6858a78

Browse files
committed
设计模式
1 parent 6cd2987 commit 6858a78

17 files changed

Lines changed: 694 additions & 17 deletions
109 Bytes
Binary file not shown.
72 Bytes
Binary file not shown.

code/Java/design-pattern/src/main/java/com/heibaiying/creational/simple_factory/PhoneFactory.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,10 @@ public Phone getPhone(String type) {
1414
public Phone getPhone(Class<? extends Phone> phoneClass) {
1515
try {
1616
return (Phone) Class.forName(phoneClass.getName()).newInstance();
17-
} catch (InstantiationException e) {
18-
e.printStackTrace();
19-
} catch (IllegalAccessException e) {
20-
e.printStackTrace();
21-
} catch (ClassNotFoundException e) {
17+
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
2218
e.printStackTrace();
2319
}
2420
return null;
2521
}
2622

27-
2823
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.heibaiying.creational.singleton;
2+
3+
/**
4+
* 使用双重检查锁来保证线程安全
5+
*/
6+
public class DoubleCheckLazySingletonSafe {
7+
8+
private static volatile DoubleCheckLazySingletonSafe instance = null;
9+
10+
private DoubleCheckLazySingletonSafe() {
11+
}
12+
13+
public static DoubleCheckLazySingletonSafe getInstance() {
14+
if (instance == null) {
15+
synchronized (DoubleCheckLazySingletonSafe.class) {
16+
if (instance == null) {
17+
instance = new DoubleCheckLazySingletonSafe();
18+
}
19+
}
20+
}
21+
return instance;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.heibaiying.creational.singleton;
2+
3+
/**
4+
* 利用枚举实现单例
5+
*/
6+
public enum EnumInstance {
7+
8+
INSTANCE;
9+
10+
private String field;
11+
12+
public String getField() {
13+
return field;
14+
}
15+
16+
public void setField(String field) {
17+
this.field = field;
18+
}
19+
20+
public static EnumInstance getInstance() {
21+
return INSTANCE;
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.heibaiying.creational.singleton;
2+
3+
import java.io.*;
4+
import java.lang.reflect.Constructor;
5+
6+
public class EnumInstanceTest {
7+
public static void main(String[] args) throws Exception {
8+
// 序列化攻击
9+
EnumInstance instance = EnumInstance.getInstance();
10+
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("EnumSingletonFile"));
11+
outputStream.writeObject(instance);
12+
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("EnumSingletonFile")));
13+
EnumInstance newInstance = (EnumInstance) inputStream.readObject();
14+
System.out.println(instance == newInstance);
15+
// 反射攻击,Enum类中只有一个两个参数的构造器:Enum(String name, int ordinal)
16+
Constructor<EnumInstance> constructor = EnumInstance.class.getDeclaredConstructor(String.class, int.class);
17+
constructor.setAccessible(true);
18+
EnumInstance enumInstance = constructor.newInstance("name", 0);
19+
System.out.println(instance == enumInstance);
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.heibaiying.creational.singleton;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* 利用静态代码块来实现饿汉式单例
7+
*/
8+
public class HungrySingleton implements Serializable {
9+
10+
private static final HungrySingleton instance;
11+
12+
static {
13+
instance = new HungrySingleton();
14+
}
15+
16+
private HungrySingleton() {
17+
if (instance != null) {
18+
throw new RuntimeException("单例模式禁止反射调用");
19+
}
20+
}
21+
22+
public static HungrySingleton getInstance() {
23+
return instance;
24+
}
25+
26+
private Object readResolve() {
27+
return instance;
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.heibaiying.creational.singleton;
2+
3+
/**
4+
* 线程不安全的懒汉式单例模式
5+
*/
6+
public class LazySingletonUnsafe {
7+
8+
private static LazySingletonUnsafe instance = null;
9+
10+
private LazySingletonUnsafe() {
11+
}
12+
13+
public static LazySingletonUnsafe getInstance() {
14+
if (instance == null) {
15+
instance = new LazySingletonUnsafe();
16+
}
17+
return instance;
18+
}
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.heibaiying.creational.singleton;
2+
3+
import java.lang.reflect.Constructor;
4+
5+
/**
6+
* 利用反射破坏单例
7+
*/
8+
public class ReflectionDamage {
9+
public static void main(String[] args) throws Exception {
10+
Constructor<HungrySingleton> constructor = HungrySingleton.class.getDeclaredConstructor();
11+
constructor.setAccessible(true);
12+
HungrySingleton hungrySingleton = constructor.newInstance();
13+
System.out.println(hungrySingleton);
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.heibaiying.creational.singleton;
2+
3+
import java.io.*;
4+
5+
/**
6+
* 利用序列化破坏单例
7+
*/
8+
public class SerializationDamage {
9+
public static void main(String[] args) throws IOException, ClassNotFoundException {
10+
HungrySingleton instance = HungrySingleton.getInstance();
11+
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("SingletonFile"));
12+
outputStream.writeObject(instance);
13+
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("SingletonFile")));
14+
HungrySingleton newInstance = (HungrySingleton) inputStream.readObject();
15+
System.out.println(instance == newInstance);
16+
}
17+
}

0 commit comments

Comments
 (0)