package observer;
import java.util.Date;
import java.util.Observable;
public class Appointment extends Observable {
private String host;
private String guest;
private Date appointmentDate;
private int status;
public Appointment(){}
public String getHost() {
return host;
}
public String getGuest() {
return guest;
}
public Date getAppointmentDate() {
return appointmentDate;
}
public int getStatus() {
return status;
}
public void createAppointment(String host, String guest, Date appointmentDate){
this.host = host;
this.guest = guest;
this.appointmentDate = appointmentDate;
this.status = 0;
setChanged();
this.notifyObservers(this);
}
public void cancelAppointment(String host, String guest, Date appointmentDate){
if(this.host != null && this.guest != null && this.appointmentDate != null && this.status != 1){
this.status = 1;
setChanged();
this.notifyObservers(this);
}
}
}
EmailService.java
package observer;
import java.util.Observable;
import java.util.Observer;
public class EmailService implements Observer {
private Observable observable;
public EmailService(Observable observable){
this.observable = observable;
observable.addObserver(this);
}
public void update(Observable o, Object arg) {
if(arg instanceof Appointment){
Appointment appointment = (Appointment)arg;
if(appointment.getStatus() ==0)
System.out.println("send a creation email to host and guest");
else
System.out.println("send a cancellation email to host and guest");
}
}
}
SMSService.java
package observer;
import java.util.Observable;
import java.util.Observer;
public class SMSService implements Observer {
private Observable observable;
public SMSService(Observable observable){
this.observable = observable;
observable.addObserver(this);
}
public void update(Observable o, Object arg) {
if(arg instanceof Appointment){
Appointment appointment = (Appointment)arg;
if(appointment.getStatus() ==0)
System.out.println("send a creation sms to host and guest");
else
System.out.println("send a cancellation sms to host and guest");
}
}
}
ObserverTest.java
package observer;
import java.util.Date;
public class ObserverTest {
public static void main(String[] args) {
Appointment appointment = new Appointment();
EmailService emailService = new EmailService(appointment);
SMSService smsService = new SMSService(appointment);
String host ="";
String guest ="";
Date appointmentDate = new Date();
appointment.createAppointment(host, guest, appointmentDate);
appointment.cancelAppointment(host, guest, appointmentDate);
}
}
you may download the source code.
No comments:
Post a Comment