工厂模式
创建对象的类称之为工厂。工厂模式用来约束对象创建行为,通常为借口创建实例,统一收敛对象创建口径,管理不同实现。
例如 spring 框架的 BeanFactory
。
# 简单工厂(静态工厂)
在工厂类中提供一个封装的静态工厂方法,用于隐藏对象初始化细节,使客户端代码可以专注于使用,而不用关心类的初始化过程。
如果一个对象的创建比较简单,推荐使用简单工厂
模式。
public interface Cache {
void get(String key);
void put(String key, String value);
}
1
2
3
4
5
6
2
3
4
5
6
public class MemoryCache implements Cache {
@Override
public void get(final String key) {
System.out.printf("get from memory: %s \r\n", key);
}
@Override
public void put(final String key, final String value) {
System.out.printf("put memory: %s - %s \r\n", key, value);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
public class RedisCache implements Cache {
@Override
public void get(final String key) {
System.out.printf("get from redis: %s \r\n", key);
}
@Override
public void put(final String key, final String value) {
System.out.printf("put redis: %s - %s \r\n", key, value);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
public enum CacheType {
MEMORY(MemoryCache::new),
REDIS(RedisCache::new),
;
private final Supplier<Cache> constructor;
CacheType(final Supplier<Cache> constructor) {
this.constructor = constructor;
}
public Supplier<Cache> getConstructor() {
return constructor;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class CacheFactory {
public static Cache createCache(CacheType type) {
return type.getConstructor().get();
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
// 测试
public class CacheApp {
public static void main(String[] args) {
final Cache cache = CacheFactory.createCache(CacheType.REDIS);
String key = "k1";
String value = "v1";
cache.put(key, value);
cache.get(key);
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 工厂方法
如果对象创建有点复杂,可以考虑用工厂方法
模式,将代码剥离,可读性会更好一些。
// 运算操作接口
public interface Operation {
int getResult(int a, int b);
}
// 加法运算
public class AddOperation implements Operation {
@Override
public int getResult(final int a, final int b) {
return a + b;
}
}
// 减法运算
public class SubOperation implements Operation {
@Override
public int getResult(final int a, final int b) {
return a - b;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public interface OperationFactory {
Operation createOperation();
}
public class AddOperationFactory implements OperationFactory {
@Override
public Operation createOperation() {
return new AddOperation();
}
}
public class SubOperationFactory implements OperationFactory {
@Override
public Operation createOperation() {
return new SubOperation();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
测试类
public class OperationApp {
public static void main(String[] args) {
OperationFactory factory = new AddOperationFactory();
final Operation operation = factory.createOperation();
int a = 10;
int b = 20;
final int result = operation.getResult(a, b);
System.out.println("result: " + result);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 抽象工厂
抽象工厂模式提供了一种方式,可以将一组具有同一主题的单独的工厂封装起来。避免创建过多的 factory 类。
参考:https://zh.wikipedia.org/zh-hans/抽象工厂 (opens new window)
public interface Button {
void click();
}
public class MacButton implements Button {
@Override
public void click() {
System.out.println("mac button click");
}
}
public class WindowsButton implements Button {
@Override
public void click() {
System.out.println("windows button click");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public interface Border {
void draw();
}
public class MacBorder implements Border {
@Override
public void draw() {
System.out.println("mac border draw");
}
}
public class WindowsBorder implements Border {
@Override
public void draw() {
System.out.println("windows border draw");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public interface UiFactory {
Button createButton();
Border createBorder();
}
public class MacUiFactory implements UiFactory {
@Override
public Button createButton() {
return new MacButton();
}
@Override
public Border createBorder() {
return new MacBorder();
}
}
public class WindowsUiFactory implements UiFactory {
@Override
public Button createButton() {
return new WindowsButton();
}
@Override
public Border createBorder() {
return new WindowsBorder();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Last Updated: 2024/04/23, 01:30:37