【GOF23设计模式】观察者模式

时间:2022-04-28 07:04:15

来源:http://www.bjsxt.com/ 
一、【GOF23设计模式】_观察者模式、广播机制、消息订阅、网络游戏对战原理

【GOF23设计模式】观察者模式

【GOF23设计模式】观察者模式

【GOF23设计模式】观察者模式

【GOF23设计模式】观察者模式

 package com.test.observer;

 import java.util.ArrayList;
import java.util.List; public class Subject {
protected List<Observer> list = new ArrayList<Observer>(); public void registerObserver(Observer obs){
list.add(obs);
}
public void removeObserver(Observer obs){
list.remove(obs);
} //通知所有的观察者更新状态
public void notjfyAllObservers(){
for (Observer obs : list) {
obs.update(this);
}
}
}
 package com.test.observer;

 public class ConcreteSubject extends Subject{
private int state; public int getState() {
return state;
} public void setState(int state) {
this.state = state;
//主题对象(目标对象)的值发生了变化,请通知所有的观察者
this.notjfyAllObservers();
}
}
 package com.test.observer;

 public interface Observer {
void update(Subject subject);
}
 package com.test.observer;

 public class ObserverA implements Observer{
private int myState;//myState需要跟目标对象的state值保持一致 public int getMyState() {
return myState;
} public void setMyState(int myState) {
this.myState = myState;
} @Override
public void update(Subject subject) {
myState = ((ConcreteSubject)subject).getState();
}
}
 package com.test.observer;

 public class Client {
public static void main(String[] args) {
//目标对象
ConcreteSubject subject = new ConcreteSubject(); //创建多个观察者
ObserverA obs1 = new ObserverA();
ObserverA obs2 = new ObserverA();
ObserverA obs3 = new ObserverA(); //将这三个观察者添加到subject对象的观察者队伍中
subject.registerObserver(obs1);
subject.registerObserver(obs2);
subject.registerObserver(obs3); //改变subject的状态
subject.setState(3000);
System.out.println("#################");
//我们看看,观察者的状态是不是也发生了变化
System.out.println(obs1.getMyState());
System.out.println(obs2.getMyState());
System.out.println(obs3.getMyState()); //改变subject的状态
subject.setState(30);
System.out.println("#################");
//我们看看,观察者的状态是不是也发生了变化
System.out.println(obs1.getMyState());
System.out.println(obs2.getMyState());
System.out.println(obs3.getMyState());
}
}
控制台输出:
#################
3000
3000
3000
#################
30
30
30

【GOF23设计模式】观察者模式

二、【GOF23设计模式】_观察者模式、obserable类和observer接口、应用场景总结

【GOF23设计模式】观察者模式

 package com.test.observer2;

 import java.util.Observable;
/**
* 目标对象
*/
public class ConcreteSubject extends Observable{
private int state; public int getState() {
return state;
} public void setState(int state) {
this.state = state;
} public void set(int s){
state = s; //目标对象的状态发生了改变 setChanged(); //表示目标对象已经做了更改
notifyObservers(state); //通知所有的观察者
}
}
 package com.test.observer2;

 import java.util.Observable;
import java.util.Observer; public class ObserverA implements Observer{
private int myState; public int getMyState() {
return myState;
} public void setMyState(int myState) {
this.myState = myState;
} @Override
public void update(Observable o, Object arg) {
myState = ((ConcreteSubject)o).getState();
}
}
 package com.test.observer2;

 public class Client {
public static void main(String[] args) {
//创建目标对象Obserable
ConcreteSubject subject = new ConcreteSubject(); //创建观察者
ObserverA obs1 = new ObserverA();
ObserverA obs2 = new ObserverA();
ObserverA obs3 = new ObserverA(); //将上面三个观察者对象添加到目标对象subject的观察者容器中
subject.addObserver(obs1);
subject.addObserver(obs2);
subject.addObserver(obs3); //改变subject对象的状态
subject.set(3000);
System.out.println("==================状态修改了");
//观察者的状态发生了变化
System.out.println(obs1.getMyState());
System.out.println(obs2.getMyState());
System.out.println(obs3.getMyState()); //改变subject对象的状态
subject.set(600);
System.out.println("==================状态修改了");
//观察者的状态发生了变化
System.out.println(obs1.getMyState());
System.out.println(obs2.getMyState());
System.out.println(obs3.getMyState());
}
}
控制台输出:
==================状态修改了
3000
3000
3000
==================状态修改了
600
600
600

【GOF23设计模式】观察者模式