我正在尝试找出一种设计模式(如果存在的话)用于我将在一堆类中重新执行某些功能的情况。以下是我面临的问题的(简化)概述:
我有一些 Java 代码来创建、更新、删除Student对象、Professor对象和Staff对象。每次创建、删除或更新此类对象时,我都想提取有关受影响对象的一些信息(例如姓名、年龄、ID)并通知外部服务。所以像:
class StudentDAO {
public Student createStudent(Student studentToCreate) {
jdbcTemplate.update(INSERT_SQL, .....);
//===> extract some info of the student
//let external service know a student was created....
}
public Student deleteStudent(Student studentToDelete) {
jdbcTemplate.update(DELETE_SQL, .....);
//===> extract some info of the student
//let external service know a student was deleted....
}
//same thing for update
}
class ProfessortDAO {
public Professor createProfessor(Professor professorToCreate) {
jdbcTemplate.update(INSERT_SQL, .....);
//===> extract some info of the professor
//let external service know a Professor was created....
}
public Student deleteProfessor(Professor professorToDelete) {
jdbcTemplate.update(DELETE_SQL, .....);
//===> extract some info of the professor
//let external service know a professor was deleted....
}
//same thing for update
}
//repeat for Staff
该示例有点做作,但假设Student, Professor,Staff没有共同的超类型。有没有一种方法可以实现此功能,而无需复制和粘贴用于提取信息的逻辑并将其发送到所有 DAO 类的 CREATE、DELETE、UPDATE 方法中?
慕勒3428872
摇曳的蔷薇
胡说叔叔
相关分类