1 引言
在工作中有没有遇到这种情况,需要根据某个条件的不同实例化不同的类,被实例化的类基本功能相同,都继承自同一父类。这种情况,就是典型的简单工厂模式应用之处。简单工厂模式,创建一个工厂,通过工厂来创建其他对象的实例。被创建的实例通常都有相同的父类。
2 业务举例
这里举一个我在实际工作当中遇到的例子。业务需求是审核公司线上商城每一笔订单的用户评论和商户评论(对用户评论的一个回复),因为两种评论的模型有一些属性的不同,用户评论对应一个实体(UserComment)一张数据表(user_comment),商户回应评论对应一个实体(MerchantComment)一张数据表(merchant_comment)。两个实体,两张数据表,对应两个mapper(持久层框架用的Mybatis Plus)。现在前端调用接口对评论进行审核,返回审核的结果,审核结果中有一个参数(identity)标识是审核用户评论还是审核商户评论,依据identity的值在service层调用不同的mapper对数据进行持久化存储。
3 简单代码实现
3.1 UserComment与MerchantComment共有属性
/**
* @program: design-pattern-learning
* @author: zgr
* @create: 2021-09-01 16:28
**/
@Data
public class CommonComment {
private Integer id;
private String commentContent;
private String commentResult;
}
3.2 UserComment
/**
* @program: design-pattern-learning
* @author: zgr
* @create: 2021-09-01 16:29
**/
@Data
public class UserComment extends CommonComment{
private String userName;
}
3.3 merchantComment
/**
* @program: design-pattern-learning
* @author: zgr
* @create: 2021-09-01 16:29
**/
@Data
public class MerchantComment extends CommonComment{
private String merchantName;
}
虽然此处的MerchantComment与UserComment字段实际没什么不同,但此代码只是一个简单的抽象,和实际的业务属性相差甚远。
3.4 定义mapper接口
/**
* @program: design-pattern-learning
* @author: zgr
* @create: 2021-09-01 16:24
**/
public interface Mapper<T> {
/**
* 存储实体到数据库
* @param entity 存储的实体
*/
void saveEntity(T entity);
}
3.5 存储商户评论审核结果的mapper
/**
* @program: design-pattern-learning
* @author: zgr
* @create: 2021-09-01 16:26
**/
public class MerchantCommentMapper implements Mapper<MerchantComment>{
@Override
public void saveEntity(MerchantComment merchantComment) {
System.out.println("存储的是审核商户回复内容," + merchantComment.getMerchantName());
}
}
3.6 存储用户评论审核结果的mapper
/**
* @program: design-pattern-learning
* @author: zgr
* @create: 2021-09-01 16:26
**/
public class UserCommentMapper implements Mapper<UserComment>{
@Override
public void saveEntity(UserComment userComment) {
System.out.println("存储的是审核用户评论内容," + userComment.getUserName());
}
3.7 简单工厂SimpleFactory
/**
* @program: design-pattern-learning
* @author: zgr
* @create: 2021-09-01 16:38
**/
public class SimpleFactory {
public static Mapper createMapper(String identity){
Mapper mapper = null;
switch (identity){
case "merchant":
mapper = new MerchantCommentMapper();
break;
case "user":
mapper = new UserCommentMapper();
break;
default:
}
return mapper;
}
public static CommonComment createCommentEntity(String identity, CommonComment commonComment){
CommonComment comment = null;
switch (identity){
case "merchant":
MerchantComment merchantComment = new MerchantComment();
merchantComment.setId(commonComment.getId());
merchantComment.setCommentContent(commonComment.getCommentContent());
merchantComment.setCommentResult(commonComment.getCommentResult());
merchantComment.setMerchantName("hello,我是商户评论回复");
comment = merchantComment;
break;
case "user":
UserComment userComment = new UserComment();
userComment.setId(commonComment.getId());
userComment.setCommentContent(commonComment.getCommentContent());
userComment.setCommentResult(commonComment.getCommentResult());
userComment.setUserName("hello,我是用户评论");
comment = userComment;
break;
default:
}
return comment;
}
}
简单工厂里面相当于有两个生产线,一个生产mapper,一个生产comment。生产comment的生产线还可以再简单一些,此处没有再做封装。
3.8 主类
/**
* @program: design-pattern-learning
* @author: zgr
* @create: 2021-09-01 16:23
**/
public class MainClass {
public static void main(String[] args) {
boolean flag = true;
while (flag) {
//模拟接口参数
Scanner sc = new Scanner(System.in);
String identity = sc.nextLine();
if (identity.equals("exit")){
flag = false;
}else {
//模拟接口接待的审核结果body
CommonComment comment = new CommonComment();
comment.setId(1);
comment.setCommentContent("审核内容");
comment.setCommentResult("yes");
Mapper mapper = SimpleFactory.createMapper(identity);
if (mapper == null) {
System.out.println("身份标识参数输入错误");
continue;
}
mapper.saveEntity(SimpleFactory.createCommentEntity(identity, comment));
}
}
}
}
3.9 运行结果
4 UML类图
UML类图是借助与idea的工具自动生成的。
4.1 mapper类图
4.2 comment类图
5 引用
1.《大话设计模式》
6 源码地址