================================= 1 Observer.java ===============================
public interface Observer {
void update(Observable o, Object arg);
}
================================= = 1 end ================================
======================== 2 EmailSendObserver.java ==========================
public class EmailSendObserver implements Observer {
//通过构造器的方式给Product注入邮件发送观察者
public EmailSendObserver(Product product) {
product.addObserver(this);
}
public void update(Observable product, Object arg) {
// TODO Auto-generated method stub
System.out.println("Product price has been changed to "
+ ((Product)product).getPrice()
+ " ;\n\t\t The notify e-mail has sent to all client.");
}
}
============================= 2 end =================================
========================= 3 SMSSendObserver.java ==============================
public class SMSSendObserver implements Observer {
//通过构造器的方式给Product注入短消息发送观察者
public SMSSendObserver(Product product) {
product.addObserver(this);
}
public void update(Observable product, Object arg) {
// TODO Auto-generated method stub
System.out.println("Product price has been changed to "
+ ((Product)product).getPrice()
+ " ;\n\t\t The notify SMS has sent to all client.");
}
}
============================= 3 end ==================================
========================== 4 Observable.java ==================================
public class Observable {
//state
private boolean changed = false;
//observer collection
private List<Observer> observers;
public Observable() {
observers = new ArrayList<Observer>(0);
}
//attach a oberver
public synchronized void addObserver(Observer o) {
if (o == null)
throw new NullPointerException();
if (!observers.contains(o)) {
observers.add(o);
}
}
//detach a oberver
public synchronized void deleteObserver(Observer o) {
observers.remove(o);
}
//trigger all observers attached to this object observer to work
public void notifyObservers() {
notifyObservers(null);
}
//trigger all observers attached to this object observer to work
public void notifyObservers(Object arg) {
synchronized (this) {
if (!changed)
return;
clearChanged();
}
for (Observer observer : observers) {
observer.update(this, arg);
}
}
//Clears the observer list so that this object no longer has any observers.
public synchronized void deleteObservers() {
observers.clear();
}
//Marks this Observable object as having been changed;
protected synchronized void setChanged() {
changed = true;
}
//Indicates that this object has no longer changed, or that it has already
//notified all of its observers of its most recent change
protected synchronized void clearChanged() {
changed = false;
}
public synchronized boolean hasChanged() {
return changed;
}
public synchronized int countObservers() {
return observers.size();
}
}
============================= 4 end =============================
============================= 5 Product.java =============================
public class Product extends Observable {
//构造器
private Product(){}
//构造器
public Product(float price){
this.price = price;
}
//商品价格
private float price;
//改变商品价格
public void changePrice(float price) {
if (this.price != price) {
this.price = price;
setChanged();
}
notifyObservers();
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
========================== 5 end ======================================
======================== 6 Test.java ======================================
public class Test {
public static void main(String[] args) {
//new product
Product product = new Product(1000);
//email observer
new EmailSendObserver(product);
//sms observer
new SMSSendObserver(product);
//chage price of product
product.changePrice(800);
}
}
=========================== 6 end ====================================