Sunny 公司欲为某高校开发一套奖励审批系统,该系统可以实现教师奖励和学生审批(Award Check)。如果教师发表的论文数超过 10 篇或学生发表论文数超过 2 篇可以评选科研奖,如果教师教学反馈分大于等于 90 分或学生平均成绩大于等于 90 分可以评选成绩优秀奖。试使用访问者模式设计该系统,以判断候选人集合中的教师或学生是否符合某种获奖要求。
一、类结构图
抽象访问者类:设计为名称为 AwardChecker 的抽象类,面向具体元素类编程,包含访问具体候选人类 Teacher 的抽象方法 visit(Teacher teacher)、访问具体候选人类 Student 的抽象方法 visit(Student student);
抽象元素类:设计为名称为 Candidate 的接口,面向抽象访问者类编程,包含接受访问的抽象方法 accept(AwardChecker awardChecker);
具体访问者类:设计为抽象访问者类的子类,包括科研奖具体访问者类 ResearchAwardChecker、优秀成绩奖具体访问者类 ScoreAwardChecker,访问者类内部分别实现抽象方法 visit(Teacher teacher)、抽象方法 visit(Student student);
具体元素类:设计为抽象元素类 Candidate 的子类,包括具体元素类 Teacher、具体元素类 Student,分类实现抽象方法 accept(AwardChecker awardChecker);
对象结构类:设计为名称为 CandidateList 的具体类,面向抽象访问者类和抽象元素类编程,持有一个抽象元素 list 的引用,包含添加抽象元素类的具体方法 add(Candidate candidate)、接受抽象访问者类访问的具体方法 accept(AwardChecker awardChecker)。
二、典型实现代码
抽象奖项评选类:抽象访问者类
// 抽象奖项评选类:抽象访问者类
public abstract class AwardChecker {
public abstract void visit(Teacher teacher);
public abstract void visit(Student student);
}
复制代码
抽象候选者类:抽象元素类
// 抽象候选者类:抽象元素类
public interface Candidate {
void accept(AwardChecker awardChecker);
}
复制代码
科研奖项评选类:具体访问者类
// 科研奖项评选类:具体访问者类
public class ResearchAwardChecker extends AwardChecker{
private String awardName = "科研奖";
@Override
public void visit(Teacher teacher) {
if (teacher.getPaperAmount() > 10) {
System.out.println(teacher.getName() + "获得" + this.awardName);
}
}
@Override
public void visit(Student student) {
if (student.getPaperAmount() > 2) {
System.out.println(student.getName() + "获得" + this.awardName);
}
}
}
复制代码
成绩优秀奖项评选类:具体访问者类
// 成绩优秀奖项评选类:具体访问者类
public class ScoreAwardChecker extends AwardChecker{
private String awardName = "成绩优秀奖";
@Override
public void visit(Teacher teacher) {
if (teacher.getTeacherScore() >= 90) {
System.out.println(teacher.getName() + "获得" + this.awardName);
}
}
@Override
public void visit(Student student) {
if (student.getScore() >= 90) {
System.out.println(student.getName() + "获得" + this.awardName);
}
}
}
复制代码
教师候选者类:具体元素类
// 教师候选者类:具体元素类
public class Teacher implements Candidate{
private String name;
private Integer paperAmount;
private Integer teacherScore;
public Teacher(String name, Integer paperAmount, Integer teacherScore) {
this.name = name;
this.paperAmount = paperAmount;
this.teacherScore = teacherScore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPaperAmount() {
return paperAmount;
}
public void setPaperAmount(Integer paperAmount) {
this.paperAmount = paperAmount;
}
public Integer getTeacherScore() {
return teacherScore;
}
public void setTeacherScore(Integer teacherScore) {
this.teacherScore = teacherScore;
}
@Override
public void accept(AwardChecker awardChecker) {
awardChecker.visit(this);
}
}
复制代码
学生候选者类:具体元素类
// 学生候选者类:具体元素类
public class Student implements Candidate{
private String name;
private Integer paperAmount;
private Integer score;
public Student(String name, Integer paperAmount, Integer score) {
this.name = name;
this.paperAmount = paperAmount;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPaperAmount() {
return paperAmount;
}
public void setPaperAmount(Integer paperAmount) {
this.paperAmount = paperAmount;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
@Override
public void accept(AwardChecker awardChecker) {
awardChecker.visit(this);
}
}
复制代码
候选者容器类:对象结构类
// 候选者容器类:对象结构类
public class CandidateList {
private ArrayList<Candidate> list = new ArrayList<Candidate>();
public void addCandidate(Candidate candidate) {
this.list.add(candidate);
}
public void accept(AwardChecker awardChecker) {
for (Candidate candidate : list) {
candidate.accept(awardChecker);
}
}
}
复制代码
客户端代码:
public class Client {
public static void main(String[] args) {
CandidateList candidateList = new CandidateList();
Candidate teacher, student;
teacher = new Teacher("张无忌", 10, 90);
student = new Student("杨过", 5, 91);
candidateList.addCandidate(teacher);
candidateList.addCandidate(student);
AwardChecker researchAwardChecker, scoreAwardChecker;
researchAwardChecker = new ResearchAwardChecker();
scoreAwardChecker = new ScoreAwardChecker();
candidateList.accept(researchAwardChecker);
candidateList.accept(scoreAwardChecker);
}
}
复制代码
编译并运行程序,输出如下结果:
杨过获得科研奖
张无忌获得成绩优秀奖
杨过获得成绩优秀奖
复制代码
评论