一、例:老板回来,我不知道
双向耦合的代码:
import java.util.ArrayList;
//观察者模式
class Secretary { // 前台秘书类
private ArrayList<StockObserver> observers = new ArrayList<StockObserver>(); // 同事列表
private String action;
public void Attach(StockObserver observer) { // 增加
observers.add(observer);
}
public void Notify() { // 通知
for (int i = 0; i < observers.size(); i++) {
observers.get(i).Update();
}
}
public String getAction() { // 得到前台状态
return action;
}
public void setAction(String action) { // 设置前台状态
this.action = action;
}
}
class StockObserver { // 看股票同事类
private String name;
private Secretary sub;
public StockObserver(String name, Secretary sub) {
super();
this.name = name;
this.sub = sub;
}
public void Update() {
System.out.println(sub.getAction() + name + "关闭股票行情,继续工作!");
}
}
public class Observer {
public static void main(String[] args) {
Secretary tongzizhe = new Secretary(); // 前台小姐tongzizhe
// 看股票的同事
StockObserver tongshi1 = new StockObserver("魏观察", tongzizhe);
StockObserver tongshi2 = new StockObserver("易观察", tongzizhe);
// 前台记下了两位同事
tongzizhe.Attach(tongshi1);
tongzizhe.Attach(tongshi2);
// 发现老板回来了
tongzizhe.setAction("老板回来了!");
// 通知两位同事
tongzizhe.Notify();
}
}
改进一:解耦实践一
abstract class Observer { // 抽象观察者类
protected String name;
protected Secretary sub;
public Observer(String name, Secretary sub) {
super();
this.name = name;
this.sub = sub;
}
public abstract void Update();
}
class StockObserver extends Observer { // 具体观察者类一:看股票的同事类
public StockObserver(String name, Secretary sub) {
super(name, sub);
// TODO Auto-generated constructor stub
}
@Override
public void Update() {
// TODO Auto-generated method stub
System.out.println(sub.getAction() + name + "关闭股票行情,继续工作!");
}
}
class NBAObserver extends Observer { // 具体观察者类二:看NBA的同事类
public NBAObserver(String name, Secretary sub) {
super(name, sub);
// TODO Auto-generated constructor stub
System.out.println(sub.getAction() + name + "关闭NBA直播,继续工作!");
}
@Override
public void Update() {
// TODO Auto-generated method stub
}
}
class Secretary { // 前台秘书类
private ArrayList<StockObserver> observers = new ArrayList<StockObserver>(); // 同事列表
private String action;
public void Attach(StockObserver observer) { // 增加
observers.add(observer);
}
public void Detach(Observer observer) { // 减少
observers.remove(observer);
}
public void Notify() { // 通知
for (int i = 0; i < observers.size(); i++) {
observers.get(i).Update();
}
}
public String getAction() { // 得到前台状态
return action;
}
public void setAction(String action) { // 设置前台状态
this.action = action;
}
}
public class Observers {
public static void main(String[] args) {
Secretary tongzizhe = new Secretary(); // 前台小姐tongzizhe
// 看股票的同事
StockObserver tongshi1 = new StockObserver("魏观察", tongzizhe);
StockObserver tongshi2 = new StockObserver("易观察", tongzizhe);
// 前台记下了两位同事
tongzizhe.Attach(tongshi1);
tongzizhe.Attach(tongshi2);
// 发现老板回来了
tongzizhe.setAction("老板回来了!");
// 通知两位同事
tongzizhe.Notify();
}
}
改进二:解耦实践二
interface Subject { // 通知者接口
void Attach(Observer observer);
void Detach(Observer observer);
void Notify();
public String getAction();
public void setAction(String action);
}
class Boss implements Subject {
private ArrayList<Observer> observers = new ArrayList<Observer>(); // 同事列表
private String action;
@Override
public void Attach(Observer observer) {
// TODO Auto-generated method stub
observers.add(observer);
}
@Override
public void Detach(Observer observer) {
// TODO Auto-generated method stub
observers.remove(observer);
}
@Override
public void Notify() {
// TODO Auto-generated method stub
for (int i = 0; i < observers.size(); i++) {
observers.get(i).Update();
}
}
public String getAction() { // 得到前台状态
return action;
}
public void setAction(String action) { // 设置前台状态
this.action = action;
}
}
class Secretary implements Subject {
private ArrayList<Observer> observers = new ArrayList<Observer>(); // 同事列表
private String action;
@Override
public void Attach(Observer observer) {
// TODO Auto-generated method stub
observers.add(observer);
}
@Override
public void Detach(Observer observer) {
// TODO Auto-generated method stub
observers.remove(observer);
}
@Override
public void Notify() {
// TODO Auto-generated method stub
for (int i = 0; i < observers.size(); i++) {
observers.get(i).Update();
}
}
@Override
public String getAction() {
// TODO Auto-generated method stub
return action;
}
@Override
public void setAction(String action) {
// TODO Auto-generated method stub
this.action = action;
}
}
abstract class Observer { // 抽象观察者类
protected String name;
protected Subject sub;
public Observer(String name, Subject sub) {
super();
this.name = name;
this.sub = sub;
}
public abstract void Update();
}
class StockObserver extends Observer { // 看股票的同事类
public StockObserver(String name, Subject sub) {
super(name, sub);
// TODO Auto-generated constructor stub
}
@Override
public void Update() {
// TODO Auto-generated method stub
System.out.println(sub.getAction() + name + "关闭股票行情,继续工作!");
}
}
class NBAObserver extends Observer { // 看NBA的同事类
public NBAObserver(String name, Subject sub) {
super(name, sub);
// TODO Auto-generated constructor stub
}
@Override
public void Update() {
// TODO Auto-generated method stub
System.out.println(sub.getAction() + name + "关闭NBA直播,继续工作!");
}
}
public class Observers {
public static void main(String[] args) {
Boss huhansan = new Boss();
// 看股票的同事
StockObserver tongshi1 = new StockObserver("魏观察", huhansan);
NBAObserver tongshi2 = new NBAObserver("易观察", huhansan);
huhansan.Attach(tongshi1);
huhansan.Attach(tongshi2);
huhansan.Detach(tongshi1);
huhansan.setAction("我胡汉三回来了!");
huhansan.Notify();
}
}
二、观察者模式
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,这个对象在状态发生变化时,会通知所有观察者对象,使他们能够自动更新自己。
abstract class Subject { // 抽象通知者类
private ArrayList<Observer> observers = new ArrayList<Observer>();
public void Attach(Observer observer) {
// TODO Auto-generated method stub
observers.add(observer);
}
public void Detach(Observer observer) {
// TODO Auto-generated method stub
observers.remove(observer);
}
public void Notify() {
// TODO Auto-generated method stub
for (int i = 0; i < observers.size(); i++) {
observers.get(i).Update();
}
}
}
abstract class Observer { // 抽象观察者类
public abstract void Update();
}
class ConcreteSubject extends Subject { // 具体通知者类
private String subjectState; // 具体被观察者状态
public String getSubjectState() {
return subjectState;
}
public void setSubjectState(String subjectState) {
this.subjectState = subjectState;
}
}
class ConcreteObserver extends Observer { // 具体观察者类
private String name;
private String observerState;
private ConcreteSubject subject;
public ConcreteObserver(ConcreteSubject subject, String name) {
super();
this.name = name;
this.subject = subject;
}
@Override
public void Update() {
// TODO Auto-generated method stub
System.out.println("观察者" + name + "的状态是:" + subject.getSubjectState());
}
public ConcreteSubject getSubject() {
return subject;
}
public void setSubject(ConcreteSubject subject) {
this.subject = subject;
}
}
public class Observers {
public static void main(String[] args) {
ConcreteSubject s = new ConcreteSubject();
s.Attach(new ConcreteObserver(s, "X"));
s.Attach(new ConcreteObserver(s, "Y"));
s.Attach(new ConcreteObserver(s, "Z"));
s.setSubjectState("ABC");
s.Notify();
}
}