简体   繁体   English

前提方法应该是公开的还是私有的?

[英]Should precondition methods be public or private?

The precondition code in methods markStudent(...) and getUnitMark(...) makes use of other method of the class: isEnrolled(...) and hasCompletedAssessments(...). 方法markStudent(...)和getUnitMark(...)中的前提条件代码使用该类的其他方法: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);
}

The question is, should methods used in precondition code, such as isEnrolled(...)and hasCompletedAssessments(...), be public or private? 问题是,前提条件代码中使用的方法(例如isEnrolled(...)和hasCompletedAssessments(...))是公开的还是私有的?

From my understanding of design by contract, the client should check if its arguments obey the precondition. 根据我对合同设计的理解,客户应检查其论点是否符合前提条件。 This means that the methods used in precondition code should be public for the client classes to do the checking. 这意味着在前提条件代码中使用的方法应该是公共的,以便客户端类可以进行检查。 However in the markStudent method, it's clearly seen that the method is checking for its own precondition. 但是,在markStudent方法中,可以清楚地看到该方法正在检查其自身的前提条件。 Can someone help? 有人可以帮忙吗?

There is no harm to making these methods public. 公开这些方法没有任何危害。 That enables a client to ensure that their calls are legal. 这样一来,客户就可以确保其通话合法。 Clients don't have to do this if they know that their call is legal. 如果客户知道自己的通话合法,则不必这样做。

It is perfectly fine for the method implementation to do its own checking as well, by calling a public method. 方法实现也可以通过调用公共方法自己进行检查,这是完全可以的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM