ObserverManager接口:
- package com.zg.service;
- public interface ObserverManager {
- public abstract void addObserver(Observer observer);
- public abstract void removeObserver(Observer observer);
- public abstract void action();
- }
package com.zg.service;
public interface ObserverManager {
public abstract void addObserver(Observer observer);
public abstract void removeObserver(Observer observer);
public abstract void action();
}
ObserverManagerImpl 接口实现类:
- package com.zg.service;
- import java.util.Vector;
- public class ObserverManagerImpl implements ObserverManager {
- Vector<Observer> observers;
- public ObserverManagerImpl() {
- observers = new Vector<Observer>();
- }
- @Override
- public void addObserver(Observer observer) {
- observers.add(observer);
- }
- @Override
- public void removeObserver(Observer observer) {
- for (Observer obs : observers) {
- if (obs.equals(observer)) {
- observers.remove(obs);
- }
- }
- }
- @Override
- public void action() {
- for (Observer obs : observers) {
- obs.startTask();
- }
- }
- }
package com.zg.service;
import java.util.Vector;
public class ObserverManagerImpl implements ObserverManager {
Vector<Observer> observers;
public ObserverManagerImpl() {
observers = new Vector<Observer>();
}
@Override
public void addObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
for (Observer obs : observers) {
if (obs.equals(observer)) {
observers.remove(obs);
}
}
}
@Override
public void action() {
for (Observer obs : observers) {
obs.startTask();
}
}
}
Observer 类:
- package com.zg.service;
- public interface Observer {
- public abstract void startTask();
- }
package com.zg.service;
public interface Observer {
public abstract void startTask();
}
OneObserver 1号观察者:
- package com.zg.service;
- public class OneObserver implements Observer {
- @Override
- public void startTask() {
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- System.out.println("我是1号观察者,我观察到数据正在变化,我要处理事情....");
- }
- });
- thread.start();
- }
- }
package com.zg.service;
public class OneObserver implements Observer {
@Override
public void startTask() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("我是1号观察者,我观察到数据正在变化,我要处理事情....");
}
});
thread.start();
}
}
TwoObserver 2号观察者:
- package com.zg.service;
- public class TwoObserver implements Observer {
- @Override
- public void startTask() {
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- System.out.println("我是2号观察者,我观察到数据正在变化,我要处理事情....");
- }
- });
- thread.start();
- }
- }
package com.zg.service;
public class TwoObserver implements Observer {
@Override
public void startTask() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("我是2号观察者,我观察到数据正在变化,我要处理事情....");
}
});
thread.start();
}
}
Test 测试一下:
- package com.zg.service;
- public class Main {
- public static void main(String[] args) {
- ObserverManager observerManager = new ObserverManagerImpl();
- OneObserver oneObserver = new OneObserver();
- TwoObserver twoObserver = new TwoObserver();
- observerManager.addObserver(oneObserver);
- observerManager.addObserver(twoObserver);
- observerManager.action();
- }
- }
package com.zg.service;
public class Main {
public static void main(String[] args) {
ObserverManager observerManager = new ObserverManagerImpl();
OneObserver oneObserver = new OneObserver();
TwoObserver twoObserver = new TwoObserver();
observerManager.addObserver(oneObserver);
observerManager.addObserver(twoObserver);
observerManager.action();
}
}
输出结果:
- 我是1号观察者,我观察到数据正在变化,我要处理事情....
- 我是2号观察者,我观察到数据正在变化,我要处理事情....