标签:
来源:http://www.bjsxt.com/
一、【GOF23设计模式】_观察者模式、广播机制、消息订阅、网络游戏对战原理
1 package com.test.observer; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class Subject { 7 protected List<Observer> list = new ArrayList<Observer>(); 8 9 public void registerObserver(Observer obs){ 10 list.add(obs); 11 } 12 public void removeObserver(Observer obs){ 13 list.remove(obs); 14 } 15 16 //通知所有的观察者更新状态 17 public void notjfyAllObservers(){ 18 for (Observer obs : list) { 19 obs.update(this); 20 } 21 } 22 }
1 package com.test.observer; 2 3 public class ConcreteSubject extends Subject{ 4 private int state; 5 6 public int getState() { 7 return state; 8 } 9 10 public void setState(int state) { 11 this.state = state; 12 //主题对象(目标对象)的值发生了变化,请通知所有的观察者 13 this.notjfyAllObservers(); 14 } 15 }
1 package com.test.observer; 2 3 public interface Observer { 4 void update(Subject subject); 5 }
1 package com.test.observer; 2 3 public class ObserverA implements Observer{ 4 private int myState;//myState需要跟目标对象的state值保持一致 5 6 public int getMyState() { 7 return myState; 8 } 9 10 public void setMyState(int myState) { 11 this.myState = myState; 12 } 13 14 @Override 15 public void update(Subject subject) { 16 myState = ((ConcreteSubject)subject).getState(); 17 } 18 }
1 package com.test.observer; 2 3 public class Client { 4 public static void main(String[] args) { 5 //目标对象 6 ConcreteSubject subject = new ConcreteSubject(); 7 8 //创建多个观察者 9 ObserverA obs1 = new ObserverA(); 10 ObserverA obs2 = new ObserverA(); 11 ObserverA obs3 = new ObserverA(); 12 13 //将这三个观察者添加到subject对象的观察者队伍中 14 subject.registerObserver(obs1); 15 subject.registerObserver(obs2); 16 subject.registerObserver(obs3); 17 18 //改变subject的状态 19 subject.setState(3000); 20 System.out.println("#################"); 21 //我们看看,观察者的状态是不是也发生了变化 22 System.out.println(obs1.getMyState()); 23 System.out.println(obs2.getMyState()); 24 System.out.println(obs3.getMyState()); 25 26 //改变subject的状态 27 subject.setState(30); 28 System.out.println("#################"); 29 //我们看看,观察者的状态是不是也发生了变化 30 System.out.println(obs1.getMyState()); 31 System.out.println(obs2.getMyState()); 32 System.out.println(obs3.getMyState()); 33 } 34 }
控制台输出: ################# 3000 3000 3000 ################# 30 30 30
二、【GOF23设计模式】_观察者模式、obserable类和observer接口、应用场景总结
1 package com.test.observer2; 2 3 import java.util.Observable; 4 /** 5 * 目标对象 6 */ 7 public class ConcreteSubject extends Observable{ 8 private int state; 9 10 public int getState() { 11 return state; 12 } 13 14 public void setState(int state) { 15 this.state = state; 16 } 17 18 public void set(int s){ 19 state = s; //目标对象的状态发生了改变 20 21 setChanged(); //表示目标对象已经做了更改 22 notifyObservers(state); //通知所有的观察者 23 } 24 }
1 package com.test.observer2; 2 3 import java.util.Observable; 4 import java.util.Observer; 5 6 public class ObserverA implements Observer{ 7 private int myState; 8 9 public int getMyState() { 10 return myState; 11 } 12 13 public void setMyState(int myState) { 14 this.myState = myState; 15 } 16 17 @Override 18 public void update(Observable o, Object arg) { 19 myState = ((ConcreteSubject)o).getState(); 20 } 21 }
1 package com.test.observer2; 2 3 public class Client { 4 public static void main(String[] args) { 5 //创建目标对象Obserable 6 ConcreteSubject subject = new ConcreteSubject(); 7 8 //创建观察者 9 ObserverA obs1 = new ObserverA(); 10 ObserverA obs2 = new ObserverA(); 11 ObserverA obs3 = new ObserverA(); 12 13 //将上面三个观察者对象添加到目标对象subject的观察者容器中 14 subject.addObserver(obs1); 15 subject.addObserver(obs2); 16 subject.addObserver(obs3); 17 18 //改变subject对象的状态 19 subject.set(3000); 20 System.out.println("==================状态修改了"); 21 //观察者的状态发生了变化 22 System.out.println(obs1.getMyState()); 23 System.out.println(obs2.getMyState()); 24 System.out.println(obs3.getMyState()); 25 26 //改变subject对象的状态 27 subject.set(600); 28 System.out.println("==================状态修改了"); 29 //观察者的状态发生了变化 30 System.out.println(obs1.getMyState()); 31 System.out.println(obs2.getMyState()); 32 System.out.println(obs3.getMyState()); 33 } 34 }
控制台输出: ==================状态修改了 3000 3000 3000 ==================状态修改了 600 600 600
标签:
原文地址:http://www.cnblogs.com/erbing/p/5802674.html