Skip to content

Instantly share code, notes, and snippets.

@nitindhar7
Created September 25, 2011 22:41
Show Gist options
  • Save nitindhar7/1241270 to your computer and use it in GitHub Desktop.
Save nitindhar7/1241270 to your computer and use it in GitHub Desktop.
Command Pattern: Student Enrollment
// Client
class Student {
private int studentId;
public void requestEnrollment(int courseId) {
Enroll enroll = new Enroll(studentId, courseId);
Invoker invoker = new Invoker();
invoker.request(enroll);
}
}
// Command
class Enroll {
private int courseId;
private int studentId;
public Enroll(int courseId, int studentId) {
this.courseId = courseId;
this.studentId = studentId;
}
}
// Invoker
class DeskAttendant {
private Queue<Enroll> enrollmentQueue;
private Registrar registrar;
public DeskAttendant() {
enrollmentQueue = new Queue<Enroll>();
registrar = new Registrar();
}
public boolean request(Enroll enroll) {
enrollmentQueue.push(enroll);
Enroll enroll = enrollmentQueue.pop();
if(registrar.enrollStudent(enroll))
return "Student successfully enrolled!";
else
return "Student does not meet the requirements!";
}
}
// Receiver
class Registrar {
public boolean enrollStudent(Enroll enroll) {
if(canStudentEnroll(enroll))
return true;
else
return false;
}
private boolean canStudentEnroll(Enroll enroll) {
// enrollment logic
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment