Obersver 是 一个接口类代表观察者,继承实现它的update(Observable arg0, Object arg1)方法就可以定义一个具体的观察者类。
Observable是一个抽象类,继承后成为一个被观察的类,通过addOberser(Observer o)注册观察者。
当Observable的对象发生改变时先调用setChanged()方法表示对象已改变,然后调用
notifyObserver(Obiect obj)
的方法通知观察者,并表示改变已完成。观察者受到通知后调用update(Observable arg0, Object arg1)方法
下面是用观察这模式模拟商品信息改变后通知商品显示界面的改变
//Action类定义了商品改变的细节状态
package wu.com.observer;
public final class Action {
public static final int PRODUCE_NAME_CHANGED=0;
public static final int PRODUCE_PRICE_CHANGED=1;
public static final int PRODUCE_COUNT_CHANGED=2;
}
//produce商品类继承Observable表示该类对象为被观察者
package wu.com.observer;
import java.util.Observable;
public class Produce extends Observable{
private String name;
private float price;
private int count;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
setChanged();
notifyObservers(Action.PRODUCE_NAME_CHANGED);
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
//设置改变点
setChanged();
//通知观察者
notifyObservers(Action.PRODUCE_PRICE_CHANGED);
}
@Override
public String toString() {
return "Produce [name=" + name + ", price=" + price + ", count="
+ count + "]";
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
setChanged();
notifyObservers(Action.PRODUCE_COUNT_CHANGED);
}
}
//NameObserver产品名称改变监听类
package wu.com.observer;
import java.util.Observable;
import java.util.Observer;
public class NameObserver implements Observer{
@Override
public void update(Observable arg0, Object arg1) {
// TODO Auto-generated method stub
if (arg1.equals(Action.PRODUCE_NAME_CHANGED)) {
System.out.println("产平名称更新");
}
}
}
//产品价格改变监听类
package wu.com.observer;
import java.util.Observable;
import java.util.Observer;
public class PriceObserver implements Observer{
@Override
public void update(Observable arg0, Object arg1) {
// TODO Auto-generated method stub
if (arg1.equals(Action.PRODUCE_PRICE_CHANGED)) {
System.out.println("产平价格更新");
}
}
}
//用来模拟显示produce信息的Jlarble也实现监听者接口
package wu.com.observer;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JLabel;
public abstract class MyJLarble extends JLabel implements Observer {
@Override
public abstract void update(Observable o, Object arg) ;
}
//测试类
package wu.com.observer;
import java.util.Observable;
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) throws InterruptedException {
final Produce p=new Produce();
JFrame frame=new JFrame("产品");
frame.setBounds(400, 300, 300, 400);
MyJLarble lbPrice=new MyJLarble(){
@Override
public void update(Observable observer,Object obj){
if (obj.equals(Action.PRODUCE_PRICE_CHANGED)) {
setText(String.valueOf(p.getPrice()));
}
}
};
lbPrice.setBounds(0, 150, 300, 150);
MyJLarble lbNmae=new MyJLarble(){
@Override
public void update(Observable observer,Object obj){
if (obj.equals(Action.PRODUCE_NAME_CHANGED)) {
setText(p.getName());
}
}
};
lbNmae.setBounds(0, 0, 300, 150);
frame.add(lbNmae);
frame.add(lbPrice);
frame.setVisible(true);
NameObserver nameObserver=new NameObserver();
PriceObserver priceObserver=new PriceObserver();
p.addObserver(priceObserver);
p.addObserver(nameObserver);
p.addObserver(lbPrice);
p.addObserver(lbNmae);
p.setName("haha");
p.setPrice(8.20f);
System.out.println(p);
Thread.sleep(5000);
p.setName("嘿!您好!");
p.setPrice(10.20f);
System.out.println(p);
}
}