Java 字段监控执行事件

2020-09-23 13:18:22 +08:00
 huifer

字段监控设计

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();
	}
}


1821 次点击
所在节点    Java
5 条回复
zhuangzhuang1988
2020-09-23 13:27:58 +08:00
aragakiyuii
2020-09-23 13:32:23 +08:00
Proxy? 类似于 JPA 的 Entity 和 Spring 中 AOP
huifer
2020-09-23 13:35:46 +08:00
@aragakiyuii 目前业务层面还是直接调用具体的 service+method , 设计上面还是采用的上文所述的哪种方式再进行开发
crclz
2020-09-23 14:37:06 +08:00
你的思路非常不错。但是已经有大佬想到更好的东西: [领域驱动设计] 的 [领域事件] 。

领域事件一般在调用实体方法时,被添加到实体的领域事件列表上,并在持久化时被发布和消费。

对于下列你提到的应用场景,领域事件都很擅长:(
字段变动.
常见新老数据变更的日志记录
字段直接会产生行为
发消息的场景.填写了用户 id 就发送消息


java 示例: https://github.com/e-commerce-sample/ecommerce-order-service
Elroooo0Y
2020-09-23 22:08:59 +08:00
没看懂想干嘛 , 问题再描述的更清楚一下 .
代码太多了 ,还是着重说下想法吧 .

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/709741

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX