GOF23 设计模式 之观察者模式
作者:互联网
观察者模式用的 场景很多 , 比如 js的dom事件的监听 ,比如聊天程序,比如serlet的监听器的实现;比如邮件订阅;
它的核心:当一个对象的状态发生变化时,它需要告知一系列的对象,使它们作出反应;
它的方式有两种:1,主动推送到客户端
2,客户端主动拉取
这里主要实现推送的方式
**************************
具体代码
接收对象接口
package observer;
public interface Observer {
void update(Subject s);
}
接收对象实现类
package observer;
public class Observer1 implements Observer {
int state;
@Override
public void update(Subject s) {
// TODO Auto-generated method stub
this.setState(((ConcreateObserver)s).getState());
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
广播者主类
package observer;
import java.util.ArrayList;
import java.util.List;
public class Subject {
private List<Observer> list = new ArrayList<Observer>();
public void registerObserver(Observer o) {
list.add(o);
}
public void removeObserver(Observer o) {
list.remove(o);
}
public void noticeAllObservers() {
for (Observer observer : list) {
observer.update(this);
}
}
}
广播者子类
package observer;
public class ConcreateObserver extends Subject{
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state= state;
this.noticeAllObservers();
}
}
客户端调用
package observer;
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
ConcreateObserver concreateObserver= new ConcreateObserver();
Observer1 o1= new Observer1();
Observer1 o2= new Observer1();
Observer1 o3= new Observer1();
concreateObserver.registerObserver(o1);
concreateObserver.registerObserver(o2);
concreateObserver.registerObserver(o3);
concreateObserver.setState(1000);
System.out.println(o1.getState());
System.out.println(o2.getState());
System.out.println(o3.getState());
concreateObserver.setState(2000);
System.out.println(o1.getState());
System.out.println(o2.getState());
System.out.println(o3.getState());
}
}
************************
java内部实现的观察者模式
广播类
package observer;
import java.util.Observable;
public class ConCreateObserver1 extends Observable{
private int state;
public void set(int s) {
state = s;
setChanged();
notifyObservers();
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
接收类
package observer;
import java.util.Observable;
import java.util.Observer;
public class Observer2 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) {
// TODO Auto-generated method stub
this.setMyState( ((ConCreateObserver1)o).getState());
}
}
代码实现
package observer;
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
// ConcreateObserver concreateObserver= new ConcreateObserver();
// Observer1 o1= new Observer1();
// Observer1 o2= new Observer1();
// Observer1 o3= new Observer1();
// concreateObserver.registerObserver(o1);
// concreateObserver.registerObserver(o2);
// concreateObserver.registerObserver(o3);
//
// concreateObserver.setState(1000);
//
// System.out.println(o1.getState());
// System.out.println(o2.getState());
// System.out.println(o3.getState());
//
// concreateObserver.setState(2000);
//
// System.out.println(o1.getState());
// System.out.println(o2.getState());
// System.out.println(o3.getState());
ConCreateObserver1 concreateObserver= new ConCreateObserver1();
Observer2 o1= new Observer2();
Observer2 o2= new Observer2();
Observer2 o3= new Observer2();
concreateObserver.addObserver(o1);
concreateObserver.addObserver(o2);
concreateObserver.addObserver(o3);
concreateObserver.set(1000);
System.out.println(o1.getMyState());
System.out.println(o2.getMyState());
System.out.println(o3.getMyState());
concreateObserver.set(2000);
System.out.println(o1.getMyState());
System.out.println(o2.getMyState());
System.out.println(o3.getMyState());
}
}
fire-fire-fox 发布了194 篇原创文章 · 获赞 10 · 访问量 6万+ 私信 关注
标签:getState,GOF23,System,观察者,concreateObserver,println,设计模式,public,out 来源: https://blog.csdn.net/wangrong111222/article/details/104127056