前置条件方法应该是 public 还是私有的?
Should precondition methods be public or private?
方法 markStudent(...) 和 getUnitMark(...) 中的前提条件代码利用了 class 的其他方法:isEnrolled(...) 和 hasCompletedAssessments(...)。
import java.util.ArrayList;
import java.util.HashMap;
public class Unit {
private String code;
private String name;
private HashMap<Integer, Student> enrolledStudents = new HashMap<Integer, Student>();
private AssessmentScheme assessmentScheme = null;
private HashMap<Assessment, HashMap<Student, Mark> > Marks
public Unit(String newCode, String newName) { code = newCode;
name = newName;
}
public void enrolStudent(Student newStudent) {
enrolledStudents.put(newStudent.getPersonID(), newStudent);
}
public void unenrolStudent(Student student) {
enrolledStudents.remove(student.getPersonID());
}
public boolean isEnrolled(Student student) {
return enrolledStudents.containsKey(student.getPersonID()); }
public ArrayList<Student> getEnrolledStudents() {
ArrayList<Student> students = new ArrayList<Student (enrolledStudents.values());
return students; }
public boolean hasCompletedAssessments(Student student) { boolean hasCompleted = true;
for (Assessment a : assessmentScheme.getAssessments()) { hasCompleted &= Marks.get(a).containsKey(student);
}
return hasCompleted; }
public void markStudent(Assessment assessment, Student student, int score, String comment) throws Exception {
/* Start Preconditions */
// Precondition: studentEnrolledInUnit
if (! isEnrolled(student)) {
} throw new Exception("Precondition violated: studentEnrolledInUnit");
// Precondition: scoreInValidRange
if ((score < 0 || (score > assessment.getWeight()) }
throw new Exception("Precondition violated: scoreInValidRange"); /* End Preconditions */
Mark mark = new Mark(assessment, student, score, comment);
Marks.get(assessment).put(student, mark);
}
问题是,前置条件代码中使用的方法,例如isEnrolled(...)和hasCompletedAssessments(...),应该是public还是private?
根据我对契约设计的理解,客户应该检查其参数是否符合前提条件。这意味着前提条件代码中使用的方法应该是 public,以便客户端 classes 进行检查。但是在 markStudent 方法中,可以清楚地看到该方法正在检查其自身的前提条件。有人可以帮忙吗?
制作这些方法没有坏处public。
这使客户能够确保他们的呼叫是合法的。
如果客户知道他们的电话是合法的,则不必这样做。
方法实现也可以通过调用 public 方法进行自己的检查。
方法 markStudent(...) 和 getUnitMark(...) 中的前提条件代码利用了 class 的其他方法:isEnrolled(...) 和 hasCompletedAssessments(...)。
import java.util.ArrayList;
import java.util.HashMap;
public class Unit {
private String code;
private String name;
private HashMap<Integer, Student> enrolledStudents = new HashMap<Integer, Student>();
private AssessmentScheme assessmentScheme = null;
private HashMap<Assessment, HashMap<Student, Mark> > Marks
public Unit(String newCode, String newName) { code = newCode;
name = newName;
}
public void enrolStudent(Student newStudent) {
enrolledStudents.put(newStudent.getPersonID(), newStudent);
}
public void unenrolStudent(Student student) {
enrolledStudents.remove(student.getPersonID());
}
public boolean isEnrolled(Student student) {
return enrolledStudents.containsKey(student.getPersonID()); }
public ArrayList<Student> getEnrolledStudents() {
ArrayList<Student> students = new ArrayList<Student (enrolledStudents.values());
return students; }
public boolean hasCompletedAssessments(Student student) { boolean hasCompleted = true;
for (Assessment a : assessmentScheme.getAssessments()) { hasCompleted &= Marks.get(a).containsKey(student);
}
return hasCompleted; }
public void markStudent(Assessment assessment, Student student, int score, String comment) throws Exception {
/* Start Preconditions */
// Precondition: studentEnrolledInUnit
if (! isEnrolled(student)) {
} throw new Exception("Precondition violated: studentEnrolledInUnit");
// Precondition: scoreInValidRange
if ((score < 0 || (score > assessment.getWeight()) }
throw new Exception("Precondition violated: scoreInValidRange"); /* End Preconditions */
Mark mark = new Mark(assessment, student, score, comment);
Marks.get(assessment).put(student, mark);
}
问题是,前置条件代码中使用的方法,例如isEnrolled(...)和hasCompletedAssessments(...),应该是public还是private?
根据我对契约设计的理解,客户应该检查其参数是否符合前提条件。这意味着前提条件代码中使用的方法应该是 public,以便客户端 classes 进行检查。但是在 markStudent 方法中,可以清楚地看到该方法正在检查其自身的前提条件。有人可以帮忙吗?
制作这些方法没有坏处public。 这使客户能够确保他们的呼叫是合法的。 如果客户知道他们的电话是合法的,则不必这样做。
方法实现也可以通过调用 public 方法进行自己的检查。