Created
September 25, 2011 22:41
-
-
Save nitindhar7/1241270 to your computer and use it in GitHub Desktop.
Command Pattern: Student Enrollment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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