Hallo,
Es soll dieses UML implementiert werden mit den folgenden Klassen und Spezifikationen:
In der Klasse Exam -> Methode subscribe habe ich es so implementiert und möchte fragen ob das so richtig ist ?
Eine Methode add habe ich dann in der Klasse ExamResultListener erstellt.
Es soll dieses UML implementiert werden mit den folgenden Klassen und Spezifikationen:
Java:
import java.util.Map;
/**
* Represents an exam with grades and a moduleId. {@link ExamResultListener}s
* (implemented as {@link Student}s) can subscribe to it, if they want a
* notification when the {@link Exam} is graded or unsubscribe if they don't
* want this anymore. Implement this according to the Observer Design Pattern.
*
* The listeners can register at most once. Multiple subsequent registrations
* are ignored.
*
* Listeners are notified in the order of registration.
*
*/
public class Exam {
private boolean graded;
private String moduleId;
private Map<Student, Double> grades;
/**
* Subscribe method according to the Observer Design Pattern. If the same
* listener subscribes multiple times, only the first registration is recorded.
*
* @param l
* the {@link ExamResultListener} that subscribes to this Exam
*/
public void subscribe(ExamResultListener l) {
// TODO Implement this function.
// Think about how to handle double subscriptions.
;
}
/**
* Unsubscribe method according to the Observer Design Pattern.
*
* @param l
* the {@link ExamResultListener} that unsubscribes from this Exam
*/
public void unsubscribe(ExamResultListener l) {
// TODO Implement this function.
// Think about how to handle unsubscribed ExamResultListeners.
}
/**
* Gets the grade for the specified Student {@code s}.
*
* @param s
* student who wants the grade
* @return the grade of s in this Exam
*/
public double getGrade(Student s) {
// TODO Implemet this funktion.
// It will return the grade of the Student s in this Exam.
return 0;
}
}
Java:
public interface ExamResultListener {
/**
* {@link Exam} notifies listeners when grades are available.
*
* @param e
* the {@link Exam} that was graded
*/
public void gradeAvailable(Exam e);
}
Java:
/**
* Represents a ExamServer, wich can save {@link Exam}s and return {@link Exam}s
* by their moduleId. Implement this according to the Singleton Design Pattern!
*
*
*/
public class ExamServer {
/**
*
* @param moduleId
* the moduleId of the {@link Exam} that should be returned.
* @return the {@link Exam} of the moduleID, or null if not found
*/
public Exam getExam(String moduleId) {
// TODO Implement this function. It will return the actual exam of this
// ExamServer.
return null;
}
/**
* Creates an Exam with the specified {@code moduleId} in this ExamServer.
*
* @param moduleId
* the moduleId of the {@link Exam}
* @return the new {@link Exam}
*/
public Exam createExam(String moduleId) {
// TODO Implement this funktion. It will create a new Exam.
return null;
}
}
Java:
public class GradingSystem {
public static void main(String[] args) {
// TODO Implement this function.
// it shall create some Exams and some Students
// and then show that the notification about
// a graded exam works properly
}
}
Java:
/**
* Represents a Student with bame and studentId. The Student can act as an
* {@link ExamResultListener} to get notified by one or more {@linkt Exam}s as
* soon as they're graded.
*
*
*/
public class Student implements ExamResultListener {
private String studentId;
private String name;
@Override
public void gradeAvailable(Exam e) {
// TODO Implement this funktion.
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
}
In der Klasse Exam -> Methode subscribe habe ich es so implementiert und möchte fragen ob das so richtig ist ?
Java:
public class Exam {
private boolean graded;
private String moduleId;
private Map<Student, Double> grades;
/**
* Subscribe method according to the Observer Design Pattern. If the same
* listener subscribes multiple times, only the first registration is recorded.
*
* @param l
* the {@link ExamResultListener} that subscribes to this Exam
*/
public void subscribe(ExamResultListener l) {
// TODO Implement this function.
// Think about how to handle double subscriptions.
l.add(moduleId, l) ;
}