需求描述: 一个实体的字段可能会有后续的行为.
字段有什么变化会有什么样的操作
上述为目前想到的字段监控可能存在的场景. 针对这样的场景做出如下设计.
接口抽象
public interface MonitoringInterface<T> {
void monitor(T t);
}
/**
* 监控:字段变更.
* 字段变更后需要做的事情
*/
public interface MonitoringFieldChange<T> {
void filedChange(T t);
}
/**
* 监控,通过字段决定后续工作.
* 例如传递这个值会后续执行什么事情.
*/
public interface MonitoringFieldWork<T> {
void fieldWork(T t);
}
三个接口的作用
综合前面的需求描述做出一个通用定义.
public class SupperMonitor<T> {
private final T data;
private MonitoringFieldChange<T> fieldChange;
private MonitoringFieldWork<T> fieldWork;
public SupperMonitor(T data, MonitoringFieldChange<T> fieldChange, MonitoringFieldWork<T> fieldWork) {
this.data = data;
this.fieldChange = fieldChange;
this.fieldWork = fieldWork;
}
public void monitor() {
this.fieldChange.filedChange(data);
this.fieldWork.fieldWork(data);
}
}
public interface FirstFieldChangeMonitoring extends MonitoringFieldChange<FirstModel> {
void nameChangeMonitor(String oldName, String newName);
default Class<?> type() {
return FirstModel.class;
}
}
public interface FirstFieldWorkMonitoring extends MonitoringFieldWork<FirstModel> {
void workByWorkField(boolean work);
default Class<?> type() {
return FirstModel.class;
}
}
public class FirstFieldChangeMonitoringImpl implements MonitoringInterface<FirstModel>, FirstFieldWorkMonitoring, FirstFieldChangeMonitoring {
@Override
public void nameChangeMonitor(String oldName, String newName) {
System.out.println("数据变更" + oldName + "\t" + newName);
}
@Override
public void filedChange(FirstModel firstModel) {
this.nameChangeMonitor("", firstModel.getName());
}
@Override
public void workByWorkField(boolean work) {
if (work) {
System.out.println("开始工作");
}
}
@Override
public Class<?> type() {
return FirstModel.class;
}
@Override
public void fieldWork(FirstModel firstModel) {
this.workByWorkField(firstModel.isWork());
}
@Override
public void monitor(FirstModel firstModel) {
SupperMonitor<FirstModel> firstModelSupperMonitor = new SupperMonitor<>(firstModel, this, this);
firstModelSupperMonitor.monitor();
}
public static void main(String[] args) {
FirstFieldChangeMonitoringImpl firstFieldChangeMonitoring = new FirstFieldChangeMonitoringImpl();
FirstModel firstModel = new FirstModel();
firstModel.setName("aaaaaa");
firstModel.setWork(true);
firstFieldChangeMonitoring.monitor(firstModel);
}
}
public interface SecondFieldWorkMonitoring extends MonitoringFieldWork<SecondModel> {
void workByWorkField(boolean work);
default Class<?> type() {
return SecondModel.class;
}
}
public class SecondFieldWorkMonitoringImpl implements SecondFieldWorkMonitoring {
@Override
public void workByWorkField(boolean work) {
System.out.println("第二个 model 的 work");
}
@Override
public void fieldWork(SecondModel secondModel) {
this.workByWorkField(secondModel.isWorking());
}
}
public class FirstFieldChangeMonitoringImpl implements MonitoringInterface<FirstModel>, FirstFieldWorkMonitoring, FirstFieldChangeMonitoring {
public static void main(String[] args) {
FirstFieldChangeMonitoringImpl firstFieldChangeMonitoring = new FirstFieldChangeMonitoringImpl();
SecondModel secondModel = new SecondModel();
secondModel.setWorking(false);
FirstModel firstModel = new FirstModel();
firstModel.setName("aaaaaa");
firstModel.setWork(true);
firstModel.setSecondModel(secondModel);
firstFieldChangeMonitoring.monitor(firstModel);
}
@Override
public void nameChangeMonitor(String oldName, String newName) {
System.out.println("数据变更" + oldName + "\t" + newName);
}
@Override
public void filedChange(FirstModel firstModel) {
// 字段变动任务
this.nameChangeMonitor("", firstModel.getName());
}
@Override
public void workByWorkField(boolean work) {
if (work) {
System.out.println("开始工作");
}
}
@Override
public void workBySecondField(SecondModel secondModel) {
SupperMonitor<SecondModel> secondModelSupperMonitor = new SupperMonitor<>(secondModel);
secondModelSupperMonitor.monitor();
}
@Override
public Class<?> type() {
return FirstModel.class;
}
@Override
public void fieldWork(FirstModel firstModel) {
// 字段行为任务
this.workByWorkField(firstModel.isWork());
this.workBySecondField(firstModel.getSecondModel());
}
@Override
public void monitor(FirstModel firstModel) {
SupperMonitor<FirstModel> firstModelSupperMonitor = new SupperMonitor<>(firstModel, this, this);
firstModelSupperMonitor.monitor();
}
}
网上找了一些相关文档, 收集了帖子中各位给出的意见, 整理了一下内容如下 https://www.yuque.com/huifer/mlnngl/vmgmq1
1
zhuangzhuang1988 2020-09-23 13:27:58 +08:00
|
2
aragakiyuii 2020-09-23 13:32:23 +08:00
Proxy? 类似于 JPA 的 Entity 和 Spring 中 AOP
|
3
huifer OP @aragakiyuii 目前业务层面还是直接调用具体的 service+method , 设计上面还是采用的上文所述的哪种方式再进行开发
|
4
crclz 2020-09-23 14:37:06 +08:00
你的思路非常不错。但是已经有大佬想到更好的东西: [领域驱动设计] 的 [领域事件] 。
领域事件一般在调用实体方法时,被添加到实体的领域事件列表上,并在持久化时被发布和消费。 对于下列你提到的应用场景,领域事件都很擅长:( 字段变动. 常见新老数据变更的日志记录 字段直接会产生行为 发消息的场景.填写了用户 id 就发送消息 ) java 示例: https://github.com/e-commerce-sample/ecommerce-order-service |
5
Elroooo0Y 2020-09-23 22:08:59 +08:00
没看懂想干嘛 , 问题再描述的更清楚一下 .
代码太多了 ,还是着重说下想法吧 . |