标签:style blog http ar 使用 java for sp 数据
package com.wsds.test; import java.util.ArrayList; import java.util.List; /** * 抽象主题类 * @author Administrator * */ public abstract class Subject { //用来保存注冊观察者的对象 private List<Observer> list = new ArrayList<Observer>(); /** * 注冊观察者对象 * @param observer 观察者对象 */ public void attach(Observer observer){ list.add(observer); System.out.println("Attached an obsever"); } /** * 删除观察者对象 * @param observer 观察者对象 */ public void detach(Observer observer){ list.remove(observer); } public void notifyObservers(String newState){ for(Observer observer: list){ observer.update(newState); } } }ConcreteSubject.java
package com.wsds.test; /** * 详细主题角色类 * @author Administrator * */ public class ConcreteSubject extends Subject{ private String state; public String getState(){ return state; } public void change(String newState){ state = newState; System.out.println("主题状态为:" + state); //状态发生改变,通知各个观察者 this.notifyObservers(state); } }Observer.java
package com.wsds.test; public interface Observer { /** * 更新接口 * @param state 更新的状态 */ public void update(String state); }ConcreteObserver.java
package com.wsds.test; public class ConcreteObserver implements Observer{ //观察者状态 private String observerState; @Override public void update(String state) { /* * 更新观察者状态,使其与目标的状态一致 */ observerState = state; System.out.println("观察者的状态为:" + state); } }Client.java
package com.wsds.test; public class Client { public static void main(String[] args) { //创建主题对象 ConcreteSubject subject = new ConcreteSubject(); //创建观察者对象 Observer observer = new ConcreteObserver(); //将观察者对象注冊到主题对象上 subject.attach(observer); //改变主题对象的状态 subject.change("new State"); } }观察者模式分为推模型和拉模型两种,上面就是推模式:
package com.wsds.test; import java.util.ArrayList; import java.util.List; /** * 抽象主题类 * @author Administrator * */ public abstract class Subject { //用来保存注冊观察者的对象 private List<Observer> list = new ArrayList<Observer>(); /** * 注冊观察者对象 * @param observer 观察者对象 */ public void attach(Observer observer){ list.add(observer); System.out.println("Attached an obsever"); } /** * 删除观察者对象 * @param observer 观察者对象 */ public void detach(Observer observer){ list.remove(observer); } public void notifyObservers(String newState){ for(Observer observer: list){ observer.update(this); } } }
package com.wsds.test; /** * 详细主题角色类 * @author Administrator * */ public class ConcreteSubject extends Subject{ private String state; public String getState(){ return state; } public void change(String newState){ state = newState; System.out.println("主题状态为:" + state); //状态发生改变,通知各个观察者 this.notifyObservers(state); } }
package com.wsds.test; public interface Observer { /** * 更新接口 * @param state 更新的状态 */ public void update(Subject subject); }
package com.wsds.test; public class ConcreteObserver implements Observer{ //观察者状态 private String observerState; @Override public void update(Subject subject) { /* * 更新观察者状态,使其与目标的状态一致 */ observerState = ((ConcreteSubject) subject).getState(); System.out.println("观察者的状态为:" + observerState); } }两种模式的比較:
package com.wsds.test; import java.util.Observable; /** * 详细主题角色类 * @author Administrator * */ public class ConcreteSubject extends Observable{ private String state; public String getState(){ return state; } public void change(String newState){ state = newState; System.out.println("主题状态为:" + state); //标记状态改变 setChanged(); //状态发生改变,通知各个观察者 this.notifyObservers(state); } }
package com.wsds.test; import java.util.Observable; import java.util.Observer; public class ConcreteObserver implements Observer{ //观察者状态 private String observerState; public ConcreteObserver(Observable o){ o.addObserver(this); } @Override public void update(Observable o, Object arg) { /* * 更新观察者状态,使其与目标的状态一致 */ observerState = ((ConcreteSubject) o).getState(); System.out.println("观察者的状态为:" + observerState); } }
标签:style blog http ar 使用 java for sp 数据
原文地址:http://www.cnblogs.com/lcchuguo/p/4037217.html