设计模式之观察者模式(二)——Java内置方式实现

观察者模式可以使用两种方式实现,另外一种见https://blog.csdn.net/wgs_93/article/details/102587641

这里使用Java内置的方式来实现。该例子中使用的场景是公司与公司所属的各事业部之间的决定通信,公司发布公告通知,各事业部在接收到通知后分别做出的处理。

被观察者(公司):

package notice2;

import java.util.Observable;

/**
 * 被观察者
 *
 * @author Scott
 * @date 2019年10月17号
 */
public class AnotherHeadquartersNotice extends Observable {

    /** 变更内容 */
    private String notice;

    public AnotherHeadquartersNotice() {
    }

    /**
     * “被观察者” 发生变更时调用 notifyObservers() 方法通知观察者
     * 发布新公告时触发通知公司下所有注册了的事业部
     *
     * @param notice 通知内容
     */
    public void setNotice(String notice) {
        this.notice = notice;
        // 调用父类的改变方法,标识状态已经发生过变换了
        setChanged();
        // 调用父类的通知方法
        notifyObservers();
    }

    public String getNotice() {
        return notice;
    }
}

公告处理接口:

package notice2;

/**
 * 公告处理接口
 * @author Scott
 * @date 2019年10月17号
 */
public interface AnotherLowerHair {
    /**
     * 下发通知接口
     */
    void lowerHair();
}

观察者A:

package notice2;

import java.util.Observable;
import java.util.Observer;

/**
 * 观察者A
 *
 * @author Scott
 * @date 2019年10月17号
 */
public class AnotherDivisionNine implements Observer, AnotherLowerHair {

    private Observable observable;
    private String notice;

    /**
     * 构造方法
     * @param observable 被观察者
     */
    public AnotherDivisionNine(Observable observable) {
        this.observable = observable;
        observable.addObserver(this);
    }

    /**
     * 更新方法
     * @param o 被观察者
     * @param arg 数据对象
     */
    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof AnotherHeadquartersNotice) {
            AnotherHeadquartersNotice anotherHeadquartersNotice = (AnotherHeadquartersNotice) o;
            this.notice = anotherHeadquartersNotice.getNotice();
            lowerHair();
        }
    }

    /**
     * 执行方法
     */
    @Override
    public void lowerHair() {
        System.out.println("第九事业部公司决策通知:" + this.notice);
    }

    public Observable getObservable() {
        return observable;
    }

    public String getNotice() {
        return notice;
    }

    public void setNotice(String notice) {
        this.notice = notice;
    }
}

观察者B:

package notice2;

import java.util.Observable;
import java.util.Observer;

/**
 * 观察者B
 *
 * @author Scott
 * @date 2019年10月16号
 */
public class AnotherDivisionEight implements Observer, AnotherLowerHair {

    private Observable observable;
    private String notice;

    /**
     * 构造方法
     *
     * @param observable 被观察者
     */
    public AnotherDivisionEight(Observable observable) {
        this.observable = observable;
        // 将自身注册为观察者
        observable.addObserver(this);
    }

    /**
     * 先判断 Observable 是否是 AnotherHeadquartersNotice
     * 再通过 get 方法获取更新内容
     * @param o 被观察者
     * @param arg 数据对象
     */
    @Override
    public void update(Observable o, Object arg) {
        if(o instanceof AnotherHeadquartersNotice) {
            AnotherHeadquartersNotice anotherHeadquartersNotice = (AnotherHeadquartersNotice)o;
            this.notice = anotherHeadquartersNotice.getNotice();
            // 执行相关操作
            lowerHair();
        }
    }

    @Override
    public void lowerHair() {
        System.out.println("第八事业部公司决策通知:" + notice);
    }
}

测试类:

package notice2;

public class Test {
    public static void main(String[] args) {
        AnotherHeadquartersNotice anotherHeadquartersNotice = new AnotherHeadquartersNotice();

        AnotherDivisionNine anotherDivisionNine = new AnotherDivisionNine(anotherHeadquartersNotice);
        AnotherDivisionEight anotherDivisionEight = new AnotherDivisionEight(anotherHeadquartersNotice);

        anotherHeadquartersNotice.setNotice("根据公司发展需要,经研究决定,对第九事业部组织结构调整如下:XXXXXXXX ");
    }
}

使用Java内置的方式来实现观察者模式,引入的包

java.util.Observable
java.util.Observer

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值