我试图找出一种设计模式,以使用(如果有的话)一种情况,在这种情况下,我将跨一堆类重新做一些功能。以下是我面临的问题的(简化)概述:
我有一些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
不共享任何公共超类型。有没有一种方法可以实现此功能,而无需复制和粘贴提取信息的逻辑并将其发送到CREATE,DELETE,UPDATE方法的所有DAO类中?
答案 0 :(得分:0)
创建ReportExternalService接口并添加键属性, 如果需要通知对象,请实现此接口。
创建一个使用参数作为ReportExternalService的方法,并使用键向外部服务报告!!
答案 1 :(得分:0)
您可以使用面向方面的编程。然后,您可以编写一个通用的切入点,该切入点与要与外部服务器进行通讯的所有方法匹配。 What is aspect-oriented programming?
答案 2 :(得分:0)
没有太多信息要继续,但是您是否考虑过将RAO <- readr::read_csv(file = "RateAddOnsExcel.csv")
RAO$...4 <- NULL
RAO$...5 <- NULL
RAO$Quarter. <- NULL
names(RAO)[1:13] = c("ProviderName","AIMNumber", "ChainName",
"RateEffectiveDate", "ComponentTotal",
"VentAddOn", "QualityAddOn",
"SpecialCareUnitAddOn", "AssessmentAddOn",
"SelectedExpenditureAddOn", "RateReduction",
"CaseMixRate", "CaseMixAssessment")
RAO$AIMNumber <- NULL
RAO$ChainName <- NULL
names(RAO)[1:13] = c("ProviderName","AIMNumber", "ChainName",
"RateEffectiveDate", "ComponentTotal",
"VentAddOn", "QualityAddOn",
"SpecialCareUnitAddOn", "AssessmentAddOn",
"SelectedExpenditureAddOn", "RateReduction",
"CaseMixRate", "CaseMixAssessment")
RAO <- RAO[-which(apply(RAO, 1, function(x)all(is.na(x)))),]
View(RAO)
remove.list <- paste(c("Myers", "Provider", "NA", "JJ"), collapse =
'|')
RAO %>% filter(!grepl(remove.list, RAO$ProviderName))
RAO %>% filter(!str_detect(RAO$ProviderName, remove.list))
用于特定类型并实现generics
来定义要从每种类型的个人访问的公共信息interface
?
您还应该能够将操作(phone, name, id)
作为参数传递。我建议为此使用(DELETE, ADD, UPDATE)
。
答案 3 :(得分:0)
您应该搜索通用存储库。您可以在此处了解更多信息:
https://www.youtube.com/results?search_query=generic+repository+java
示例代码:
git add commondoc.txt
git commit -m 'Add commondoc.txt symlink from my-common-repo'