Skip to content

Instantly share code, notes, and snippets.

@MinCha
Created August 16, 2014 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MinCha/5f979a574a96afd850e5 to your computer and use it in GitHub Desktop.
Save MinCha/5f979a574a96afd850e5 to your computer and use it in GitHub Desktop.
// 2. Double Dispatch 방식 리팩토링
NewPrescription prescription = new NewPrescription();
assertEquals("two pill a day", prescription.getPrescription(new SeriousPatientStatus(), new AdultPatientType()));
assertEquals("one pill a day", prescription.getPrescription(new MildPatientStatus(), new AdultPatientType()));
assertEquals("one pill a day", prescription.getPrescription(new SeriousPatientStatus(), new ChildPatientType()));
assertEquals("half pill a day", prescription.getPrescription(new MildPatientStatus(), new ChildPatientType()));
public class NewPrescription {
public String getPrescription(PatientStatus status, PatientType type) {
return status.getPrescriptionWith(type);
}
public double getMaxIngestionPerDay(PatientStatus status, PatientType type) {
return status.getMaxIngrestionPerDayWith(type);
}
}
public class SeriousPatientStatus implements PatientStatus {
@Override
public String getPrescriptionWith(PatientType type) {
return type.getPrescriptionSerious();
}
@Override
public double getMaxIngrestionPerDayWith(PatientType type) {
return type.getMaxIngrestionSerious();
}
}
public class ChildPatientType implements PatientType {
@Override
public String getPrescriptionSerious() {
return "one pill a day";
}
@Override
public double getMaxIngrestionSerious() {
return 1;
}
@Override
public String getPrescriptionMild() {
return "half pill a day";
}
@Override
public double getMaxIngrestionMild() {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment