一:被观察者(接口和实现)
①:被观察者(接口)
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("老板来喽,大家请认真工作");
}
}