java 观察者模式(大话设计模式)

观察者模式:又叫做发布-订阅模式。它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使他们能够自动更新自己。【继承+组合 】

java 观察者模式(大话设计模式)_第1张图片

/**
 * 主题或抽象通知者
 * 
 * @author administrator
 *
 */
public abstract class Subject {
    private List<Observer> observers = new ArrayList<Observer>();

    public void attach(Observer observer) {
	observers.add(observer);
    }

    public void detach(Observer observer) {
	observers.remove(observer);
    }

    public void notifyObserver() {
	for (Observer observer : observers) {
	    observer.update();
	}
    }
}
/**
 * 具体主题或通知者
 * 
 * @author administrator
 *
 */
public class ConcreteSubject extends Subject {
    private String subjectState;

    public String getSubjectState() {
	return subjectState;
    }

    public void setSubjectState(String subjectState) {
	this.subjectState = subjectState;
    }
}

/**
 * 抽象观察者
 * 
 * @author administrator
 *
 */
public abstract class Observer {

    public abstract void update();
}

/**
 * 具体观察者
 * 
 * @author administrator
 *
 */
public class ConcreteObserver extends Observer {

    private String name;
    private String observerState;
    private ConcreteSubject concreteSubject;

    public ConcreteObserver(ConcreteSubject concreteSubject, String name) {
	this.setName(name);
	this.setConcreteSubject(concreteSubject);
    }

    @Override
    public void update() {
	this.setObserverState(concreteSubject.getSubjectState());
	System.out.println("观察者" + this.getName() + "的新状态是"
		+ this.getObserverState());
    }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }

    public String getObserverState() {
	return observerState;
    }

    public void setObserverState(String observerState) {
	this.observerState = observerState;
    }

    public ConcreteSubject getConcreteSubject() {
	return concreteSubject;
    }

    public void setConcreteSubject(ConcreteSubject concreteSubject) {
	this.concreteSubject = concreteSubject;
    }

}
/**
 * 观察者模式客户端代码
 * 
 * @author administrator
 *
 */
public class ObserverClient {
    public static void main(String[] args) {
	ConcreteSubject concreteSubject = new ConcreteSubject();

	concreteSubject.attach(new ConcreteObserver(concreteSubject, "X"));
	concreteSubject.attach(new ConcreteObserver(concreteSubject, "Y"));
	concreteSubject.attach(new ConcreteObserver(concreteSubject, "Z"));

	concreteSubject.setSubjectState("ABC");
	concreteSubject.notifyObserver();

    }

}


案例 老板回来了

/**
 * 观察者模式(Observer)
 * 通知者接口
 */
public interface Subject {

    void attach(Observer observer);

    void detach(Observer observer);

    void notice();

    String getAction();

    void setAction(String action);

}
/**
 * 观察者模式(Observer)
 * 秘书类
 */
public class Secretary implements Subject {

    // 同事列表
    private List<Observer> observers = new ArrayList<>();
    private String action;

    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notice() {
        for (Observer o : observers) {
            o.update();
        }
    }

    @Override
    public String getAction() {
        return action;
    }

    @Override
    public void setAction(String action) {
        this.action = action;
    }

}
/**
 * 观察者模式(Observer)
 * 老板类
 */
public class Boss implements Subject {

    // 同事列表
    private List<Observer> observers = new ArrayList<>();
    private String action;

    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notice() {
        for (Observer o : observers) {
            o.update();
        }
    }

    @Override
    public String getAction() {
        return action;
    }

    @Override
    public void setAction(String action) {
        this.action = action;
    }

}

/**
 * 观察者模式(Observer)
 * 抽象观察者
 */
public abstract class Observer {

    protected String name;
    protected Subject subject;

    public Observer(String name, Subject subject) {
        this.name = name;
        this.subject = subject;
    }

    public abstract void update();

}
/**
 * 观察者模式(Observer)
 * 看股票的同事
 */
public class StockObserver extends Observer {

    public StockObserver(String name, Subject subject) {
        super(name, subject);
    }

    @Override
    public void update() {
        System.out.println(subject.getAction() + name + " 关闭股票行情,继续工作!");
    }

}
/**
 * 观察者模式(Observer)
 * 看NBA的同事
 */
public class NBAObserver extends Observer {

    public NBAObserver(String name, Subject subject) {
        super(name, subject);
    }

    @Override
    public void update() {
        System.out.println(subject.getAction() + name + " 关闭NBA直播,继续工作!");
    }

}
/**
 * 观察者模式(Observer)
 * 客户端main方法
 */
public class Client {

    public static void main(String[] args) {
        // 老板
        Boss boss = new Boss();
        // 看股票的同事
        StockObserver worker1 = new StockObserver("舒克", boss);
        // 看NBA的同事
        NBAObserver worker2 = new NBAObserver("贝塔", boss);

        boss.attach(worker1);
        boss.attach(worker2);

        boss.detach(worker1);

        // 老板回来
        boss.setAction("我胡汉三回来啦!");

        // 发出通知
        boss.notice();
    }

}

你可能感兴趣的:(设计模式)