中科院云平台架构师,专注于数字化、智能化,技术方向:云、Linux内核、AI、MES/ERP/CRM/OA、物联网、传感器、大数据、ML、微服务。
分类: Java
2013-12-06 15:48:16
说明:观察者执行观察行为,添加被观察对象到观察列表中。
案1:
利用JDK的类:
package hiya.observer.jdk;
import java.util.Observable;
import java.util.Observer;
// this is a subject(观察者:执行观察行为的人)
public class BeingWatched extends Observable{
void doBusiness() {
for (int period = 5; period >= 0; period--) {
if (true) {
super.setChanged();
}
notifyObservers( new Integer ( period ) );
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("Sleep interrupeted");
}
}
}
public static void main(String [] args) {
//创建一个观察者 chf@tsinghua.org.cn
BeingWatched subject = new BeingWatched();
//创建两个被观察者
Observer mailObserver = new MailObserver();
Observer countObserver = new CountObserver();
//把两个被观察者加到观察者列表要观察的对象列表中
subject.addObserver(mailObserver);
subject.addObserver(countObserver);
//执行业务操作
subject.doBusiness();
}
}
package hiya.observer.jdk;
import java.util.Observable;
import java.util.Observer;
public class CountObserver implements Observer {
@Override
public void update(Observable o, Object arg) {
System.out.println("Update() called, count is " + ((Integer) arg).intValue());
}
}
package hiya.observer.jdk;
import java.util.Observable;
import java.util.Observer;
public class MailObserver implements Observer {
@Override
public void update(Observable o, Object arg) {
System.out.println("You is sending mail!....OK");
}
}
执行结果:
Update() called, count is 5
You is sending mail!....OK
Update() called, count is 4
You is sending mail!....OK
Update() called, count is 3
You is sending mail!....OK
Update() called, count is 2
You is sending mail!....OK
Update() called, count is 1
You is sending mail!....OK
Update() called, count is 0
You is sending mail!....OK
比较简单容易理解.
以下是自己写的:(1)用监听 (2)大部分相同,只修改不一样的地方
package hiya.observer;
public class ObserverPatternTest {
/**
* [メソッドの説明を書きましょう]
* @param args
*/
public static void main(String[] args) {
Subject subject1 = new Subject();
Subject subject2 = new Subject();
subject1.addPaymentListener(new Observer(){
public String Action(SubjectEvent subjectEvent){
return subjectEvent.getText() + " with cash. ";
}
});
subject2.addPaymentListener(new Observer(){
public String Action(SubjectEvent subjectEvent){
return subjectEvent.getText() + " with credit. ";
}
});
subject1.setAmount(777);
subject2.setAmount(888);
System.out.println(subject1.sellAction());
System.out.println(subject2.sellAction());
}
}
package hiya.observer;
import java.util.EventListener;
public interface Observer extends EventListener{
public String Action(SubjectEvent subjectEvent);
}
package hiya.observer;
import java.util.ArrayList;
public class Subject {
private double amount;
private ArrayList eventListenerLst;
public void addPaymentListener(Observer observer){
if (eventListenerLst == null){
eventListenerLst = new ArrayList();
}
if (!eventListenerLst.contains(observer)){
eventListenerLst.add(observer);
}
}
public void removePaymentListener(Observer observer){
if (eventListenerLst != null && eventListenerLst.contains(observer)){
eventListenerLst.remove(observer);
}
}
public String sellAction(){
String sellAction = null;
String preSellAction = "You has payment ";
sellAction = preSellAction;
sellAction += amount;
SubjectEvent subjectEvent = new SubjectEvent(this, sellAction);
sellAction = activeEvent(subjectEvent);
sellAction += " I am selecting warehouse, Please waiting...";
return sellAction;
}
private String activeEvent(SubjectEvent subjectEvent) {
//String text = subjectEvent.getText();
String text = "";
if (eventListenerLst != null){
for (int i=0; i< eventListenerLst.size(); i++){
Observer observer = (Observer)eventListenerLst.get(i);
text += observer.Action(subjectEvent);
}
}
return text;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
package hiya.observer;
import java.util.EventObject;
public class SubjectEvent extends EventObject {
private String text;
// http://hiyachen.blog.chinaunix.net
public SubjectEvent(Object source, String text) {
super(source);
setText(text);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
执行结果
You has payment 777.0 with cash. I am selecting warehouse, Please waiting...
You has payment 888.0 with credit. I am selecting warehouse, Please waiting...