观察者模式

一:被观察者(接口和实现)

①:被观察者(接口)

package com.myrepository.mytemplate.designpattern.observerpattern;

/**
 * 被观察者(抽象主题角色,要有删除和新增观察者的能力和通知观察者的能力)
 */
public interface ObservedInterface {

    /**
     * 添加观察者
     * @param observerInterface
     * @return
     */
    boolean addObserver(ObserverInterface observerInterface);

    /**
     * 删除观察者
     * @param observerInterface
     * @return
     */
    boolean delObserver(ObserverInterface observerInterface);

    /**
     * 向观察者通知消息
     * @param message
     * @return
     */
    boolean notifyObserver(String message);

}

②:被观察者(实现)

package com.myrepository.mytemplate.designpattern.observerpattern.impl;

import com.myrepository.mytemplate.designpattern.observerpattern.ObservedInterface;
import com.myrepository.mytemplate.designpattern.observerpattern.ObserverInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ObservedInterfaceImpl implements ObservedInterface {

    /**存放观察者集合*/
    @Autowired
    private List<ObserverInterface> observerInterfaces;

    @Override
    public boolean addObserver(ObserverInterface observerInterface) {
        return observerInterfaces.add(observerInterface);
    }

    @Override
    public boolean delObserver(ObserverInterface observerInterface) {
        return observerInterfaces.remove(observerInterface);
    }

    @Override
    public boolean notifyObserver(String message) {
        boolean flag = false;
        //临时增加一个替补
        ObserverInterface observerInterfaceTemp = new ObserverInterface() {
            @Override
            public boolean receiveMessage(String message) {
                System.err.println("我是三号临时替补员:"+message);
                return true;
            }
        };
        addObserver(observerInterfaceTemp);
        for (ObserverInterface observerInterface:observerInterfaces) {
            flag = observerInterface.receiveMessage(message);
            if(!flag){
                log.error(String.format("观察者%s未成功的接收到消息",observerInterface.getClass()));
            }
        }
        //执行完之后要把临时的删除
        delObserver(observerInterfaceTemp);
        return flag;
    }
}

二:观察者(接口和实现)

①:观察者(接口)

package com.myrepository.mytemplate.designpattern.observerpattern;

/**
 * 观察者(抽象观察者)
 */
public interface ObserverInterface {

    /**
     * 接收被观察者发送的消息
     * @param message
     * @return
     */
    boolean receiveMessage(String message);
}

②:实现类1

package com.myrepository.mytemplate.designpattern.observerpattern.impl;

import com.myrepository.mytemplate.designpattern.observerpattern.ObserverInterface;
import org.springframework.stereotype.Service;

/**
 * 一号观察者
 */
@Service
public class ObserverOneInterfaceImpl implements ObserverInterface {

    @Override
    public boolean receiveMessage(String message) {
        System.err.println("我是一号观察员,收到消息");
        return true;
    }

}

③:实现类2

package com.myrepository.mytemplate.designpattern.observerpattern.impl;

import com.myrepository.mytemplate.designpattern.observerpattern.ObserverInterface;
import org.springframework.stereotype.Service;

/**
 * 二号观察者
 */
@Service
public class ObserverTwoInterfaceImpl implements ObserverInterface {

    @Override
    public boolean receiveMessage(String message) {
        System.err.println("我是二号观察员,收到消息");
        return true;
    }

}

三:测试类(和打印结果)

package com.myrepository.mytemplate.controller;

import com.myrepository.mytemplate.designpattern.observerpattern.ObservedInterface;
import com.myrepository.mytemplate.designpattern.observerpattern.ObserverInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class ObserverPatternController {

    @Autowired
    private ObservedInterface observedInterface;

    @Resource
    private ObserverInterface observerTwoInterfaceImpl;

    @GetMapping("testObserverSendMessage")
    public void testObserverSendMessage(){

        //删除二号观察员
        observedInterface.delObserver(observerTwoInterfaceImpl);

        observedInterface.notifyObserver("老板来喽,大家请认真工作");
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值