YEN_CSDN的博客

如果真的相信什么,就要用尽全力去让它发生。

观察者模式-Observer Pattern

前言

Define a one-to-many dependency between objects so that when one object changes state,all its dependents are notified and updated automatically.

定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,所有依赖于它的对象都会得到通知并被自动更新.

GitHub源码:

https://github.com/YEN-GitHub/PatternDemo/tree/master/src/BehavioralPattern2/ObserverPattern

题目

观察者有游戏观察者(GameObserver)和视频观察者(VideoObserver)。
观察者中的更新函数(updata)只需输出具体Subject对象的状态和状态改变语句即可。
目标对象(Subject)有哨兵(Sentry)和老师(teacher),一个目标对象可以对应多个和多种观察者对象,
当客服端目标对象状态设置为("高老师来了"),调用通知函数(notifyObserver)通知所有对应的观察者对象执行更新函数。
其中老师(teacher)的状态还包括一个惩罚(panish)函数,惩罚(panish)函数输出如下“XXX  XXX(所有惩罚人的名字)把观察者模式的结构图抄写10遍”
完成状态更新。

UML类图

这里写图片描述

代码

package BehavioralPattern2.ObserverPattern.demo1;

/**
 * Created by yangenneng on 2017-06-14 10:37
 * Abstract:抽象观察者角色
 */
public abstract class Observer {



    protected String stuName;//学生姓名
    protected Subject subject;//哨兵

    /*构造函数*/
    Observer(String stuName,Subject subject){
        this.stuName=stuName;
        this.subject=subject;
    }

    /*改变状态*/
    public abstract void update();

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Subject getSubject() {
        return subject;
    }

    public void setSubject(Subject subject) {
        this.subject = subject;
    }

}
package BehavioralPattern2.ObserverPattern.demo1;

import java.util.ArrayList;

/**
 * Created by yangenneng on 2017-06-14 10:37
 * Abstract:抽象主题角色-即哨兵
 */
public abstract class Subject {

    protected String msg="";//通知信息

    Subject(String msg){
        this.msg=msg;
    }

    /*存放所有待通知的观察者*/
    protected ArrayList<Observer> arrayList=new ArrayList<>();

    /*增加被观察者*/
    public void addObserver(Observer obj) {arrayList.add(obj);}

    /*移除被观察者*/
    public void removeObserver(Observer obj){arrayList.remove(obj);}

    /*通知函数*/
    public abstract void notifyObserver();

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }


}
package BehavioralPattern2.ObserverPattern.demo1;

/**
 * Created by yangenneng on 2017-06-14 11:01
 * Abstract:游戏观察者
 */
public class GameObserver extends Observer {

    GameObserver(String name,Subject subject){super(name,subject);}

    @Override
    public void update() {
        System.out.println(this.stuName+"切换状态,由游戏状态变为做实验状态");
    }

}
package BehavioralPattern2.ObserverPattern.demo1;

/**
 * Created by yangenneng on 2017-06-14 11:02
 * Abstract:视频观察者
 */
public class VideoObserver extends Observer {

    VideoObserver(String name,Subject subject){super(name,subject);}

    @Override
    public void update() {
        System.out.println(this.stuName+"切换状态,由视频状态变为做实验状态");
    }
}
package BehavioralPattern2.ObserverPattern.demo1;

/**
 * Created by yangenneng on 2017-06-14 11:03
 * Abstract:好人哨兵
 */
public class SentrySubject extends Subject{

    SentrySubject(String msg) {
        super(msg);
    }

    /*通知函数*/
    public void notifyObserver(){
        System.out.println(msg);
        for (Observer observer:arrayList){
            observer.update();
        }
    }

}
package BehavioralPattern2.ObserverPattern.demo1;

/**
 * Created by yangenneng on 2017-06-14 11:06
 * Abstract:老师-哨兵
 */
public class TeacherSubject extends Subject{

    TeacherSubject(String msg) {
        super(msg);
    }

    /*通知函数*/
    public void notifyObserver(){
        System.out.println(msg);
        for (Observer observer:arrayList){
            observer.update();
        }
        panish();//惩罚
    }

    /*惩罚学生函数*/
    public void panish(){
        for (Observer observer:arrayList){
            System.out.print(observer.getStuName()+ " ");
        }
        System.out.println("罚抄观察者模式10遍");
    }

}
package BehavioralPattern2.ObserverPattern.demo1;

/**
 * Created by yangenneng on 2017-06-14 11:10
 * Abstract:客户端测试
 */
public class Client {
    public static void main(String[] args) {
        Subject subject=new SentrySubject("");

        Observer stu1=new GameObserver("张三",subject);
        Observer stu2=new VideoObserver("李四",subject);

        subject.addObserver(stu1);
        subject.addObserver(stu2);

        subject.notifyObserver();

        System.out.println("-------------------------------------");

        subject=new TeacherSubject("高老师来了");

        subject.addObserver(stu1);
        subject.addObserver(stu2);
        subject.notifyObserver();
    }
}

运行结果

这里写图片描述

补充说明

模式可以用接口实现,这里用的是抽象类。用接口的话只要把一些属性放到具体的子类里去实现就行了.

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YEN_CSDN/article/details/73203833
所属专栏: 编程体系相关基础
上一篇责任链模式 - Chain of Responsibility Pattern
下一篇备忘录模式-Memento Pattern
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭