需求描述: 一个实体的字段可能会有后续的行为.
字段有什么变化会有什么样的操作
上述为目前想到的字段监控可能存在的场景. 针对这样的场景做出如下设计.
接口抽象
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();
}
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.