banner
NEWS LETTER

用Spring实现工厂模式,简单实用

Scroll down

用Spring实现工厂模式,简单实用

主要是两种方式:

  1. 借助Spring容器获取bean
  2. 初始化bean时主动注册到工厂

借助Spring容器获取bean

Spring容器本身可以理解成为是一个bean的工厂,通过bean名获取单例的bean。那么我们也可以借助Spring容器本身的特点,来创建我们的工厂。

首先定义服务类型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Getter
@AllArgsConstructor
public enum CommonTypeEnum {
/**
* 点赞
*/
LIKE(1),
/**
* 取消点赞
*/
CANCEL_LIKE(2),
/**
* 发布文章
*/
PUBLISH_ARTICLE(3),
/**
* 删除文章
*/
DELETE_ARTICLE(4);

private int code;

}

创建一个接口,定义两个方法:一个方法是指定其生产的service类型,另一个方法假定是我们要执行的主逻辑,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public interface CommonHandlerService {
/**
* 获取当前服务类型
*
* @return
*/
CommonTypeEnum getCommonTypeEnum();

/**
* 执行流程
*
* @param commonMessage
* @return
*/
boolean process(String commonMessage);
}

然后,让我们想要创建的类实现定义的接口就可以了,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 点赞消息处理
*
* @author xiaobai
* @date 2021-08-28
*/
public class LikeHandler implements CommonHandlerService {

@Override
public CommonTypeEnum getCommonTypeEnum() {
return CommonTypeEnum.LIKE;
}

@Override
public boolean process(String commonMessage) {
// do sth
return true;
}
}

之后我们创建一个工厂类。

  1. 使用一个map来维护工厂。
  2. 使用ApplicationContext类的getBeansOfType方法获取指定类型的全部bean,该方法返回的是一个key为服务名,value为类实例的map。
  3. 通过@PostConstruct注解在启动后来填充我们的map。
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
35
36
/**
* 通过applicationContext协助创建工厂
*
* @author xiaobai
* @date 2021-08-28
*/
@Component(value = "aCommonHandlerFactory")
public class CommonHandlerFactory {

@Autowired
private ApplicationContext applicationContext;

protected Map<CommonTypeEnum, CommonHandlerService> commonServiceMap = Maps.newHashMap();

@PostConstruct
public void init() {
Map<String, CommonHandlerService> commonHandlerServiceMap = applicationContext.getBeansOfType(CommonHandlerService.class);
for (Map.Entry<String, CommonHandlerService> entry : commonHandlerServiceMap.entrySet()) {
CommonHandlerService commonHandlerService = entry.getValue();
CommonTypeEnum commonTypeEnum = commonHandlerService.getCommonTypeEnum();
if (Objects.isNull(commonTypeEnum)) {
throw new IllegalArgumentException("错误的CommonHandlerService类型,beanName:" + entry.getKey());
}

if (Objects.nonNull(commonServiceMap.get(commonTypeEnum))) {
throw new IllegalArgumentException("重复的CommonHandlerService类型,beanName:" + entry.getKey());
} else {
commonServiceMap.put(commonTypeEnum, commonHandlerService);
}
}
}

public CommonHandlerService getProcessor(CommonTypeEnum operationType) {
return commonServiceMap.get(operationType);
}
}

之后我们可以使用工厂类的getProcessor方法,来获取我们想要的service了。

初始化bean时主动注册到工厂

第二种方式,是在实现类初始化时,主动的去填充到工厂里。

我们依旧使用上一个例子里的服务类型定义。

然后创建一个接口,只定义一个执行流程的方法:

1
2
3
4
5
6
7
8
9
public interface CommonHandlerService {
/**
* 执行流程
*
* @param commonMessage
* @return
*/
boolean process(String commonMessage);
}

创建工厂类。这里我们依旧是用一个map来维护映射关系,但是只提供了一个register接口,供要创建的实现类主动调用注册到工厂。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 通过注册方式创建工厂
*
* @author xiaobai
* @date 2021-08-28
*/
@Component
public class CommonHandlerFactory {
protected Map<Integer, CommonHandlerService> commonServiceMap = new HashMap<>();

public void register(CommonTypeEnum commonTypeEnum, CommonHandlerService commonHandlerService) {
commonServiceMap.put(commonTypeEnum.getCode(), commonHandlerService);
}

public CommonHandlerService getProcessor(int operationType) {
return commonServiceMap.get(operationType);
}

}

创建第一步接口的实现类,需要用@PostConstruct注解的方法调用工厂类的register接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Component
public class LikeHandler implements CommonHandlerService {

@Autowired
private CommonHandlerFactory processorFactory;

@PostConstruct
public void init() {
processorFactory.register(CommonTypeEnum.LIKE, this);
}

@Override
public boolean process(String commonMessage) {
// do sth
return true;
}
}

最后也是通过使用工厂类的getProcessor方法,来获取我们想要的service。

总结

第一个方式,省心,创建实现类的时候有更好的约束,而且维护工厂的map时都是自动的,不用像方式二里还需要自己去注册。

第二个方式,灵活可配置。如果实现类不想加入工厂,在创建时不注册到工厂即可。

其他文章
请输入关键词进行搜索