代码实现1
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public interface Observer { void update(Subject subject); }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class ObserverA implements Observer{ private int myState; //myState需要跟目标对象的state保持一致 @Override public void update(Subject subject) { myState = ((ConcreteSubject)subject).getState(); } public int getMyState() { return myState; } public void setMyState(int myState) { this.myState = myState; } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 notifyAllObservers(){ for (Observer obs:list) { obs.update(this); } } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class ConcreteSubject extends Subject{ private int state; public int getState() { return state; } public void setState(int state) { this.state = state; //主题对象发生了变化,通知所有的观察者 this.notifyAllObservers(); } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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(obs1.getMyState()); System.out.println(obs2.getMyState()); System.out.println(obs3.getMyState()); } }
代码实现2
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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(); } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//目标对象 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); //通知所有的观察者 } }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public class Client { public static void main(String[] args) { //创建目标对象Observer 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(5000); //查看观察者的状态是否变化 System.out.println(obs1.getMyState()); System.out.println(obs2.getMyState()); System.out.println(obs3.getMyState()); } }