介绍
观察者模式又称为源收听模式,观察者与被观察者之间是一对多的关系,简而言之,观察者模式就是,当被观察者发生变动,系统会主动他通知观察者,从而做一些处理。
场景
接下来实现一个场景:演员与观众,演员刀哥在台上表演,只要刀哥一表演到绝活,台下观众便会发出雷鸣般的掌声。
事件抽象类
abstract class Event<T>{
public abstract T getSource();
}
事件类
class PerformingSkillsEvent extends Event<Performer>{
long timestamp;
String info;
Performer source;
PerformingSkillsEvent(long timestamp,String info,Performer source){
this.timestamp=timestamp;
this.info=info;
this.source=source;
}
@Override
public Performer getSource(){
return this.source;
}
}
观察者接口
interface Observe{
/**
* 鼓掌
* /
void actionClapping(PerformingSkillsEvent event);
}
观察者
class Audience1 implements Observe {
@Override
public void actionClapping(PerformingSkillsEvent event) {
System.out.println("观众1号说:太棒了太棒了");
}
}
class Audience2 implements Observe {
@Override
public void actionClapping(PerformingSkillsEvent event) {
System.out.println("观众2号说:什么呀不如我");
}
}
被观察者
class Performer{
/**
* 充当被观察者的属性
*/
boolean isWakeUp=false;
List<Observe> observeList=new ArrayList<>();
public void addListener(Observe observer){
observeList.add(observer);
}
PerformingSkillsEvent performingSkillsEvent=new PerformingSkillsEvent(System.currentTimeMillis(),"info",this);
public void performingSkills(){
isWakeUp=true;
for(Observe observe:observeList){
observe.actionClapping(performingSkillsEvent);
}
}
}
测试
public static void main(String[] args){
Performer performer = new Performer();
performer.addListener(new Audience1());
performer.addListener(new Audience2());
performer.performingSkills();
}
类图
完整代码
import java.util.ArrayList;
import java.util.List;
abstract class Event<T> {
public abstract T getSource();
}
class PerformingSkillsEvent extends Event<Performer> {
long timestamp;
String info;
Performer source;
PerformingSkillsEvent(long timestamp, String info, Performer source) {
this.timestamp = timestamp;
this.info = info;
this.source = source;
}
@Override
public Performer getSource() {
return this.source;
}
}
interface Observe {
/**
* 鼓掌
*/
void actionClapping(PerformingSkillsEvent event);
}
class Audience1 implements Observe {
@Override
public void actionClapping(PerformingSkillsEvent event) {
System.out.println("观众1号说:太棒了太棒了");
}
}
class Audience2 implements Observe {
@Override
public void actionClapping(PerformingSkillsEvent event) {
System.out.println("观众2号说:什么呀不如我");
}
}
class Performer {
/**
* 充当被观察者的属性
*/
boolean isWakeUp = false;
List<Observe> observeList = new ArrayList<>();
public void addListener(Observe observer) {
observeList.add(observer);
}
PerformingSkillsEvent performingSkillsEvent = new PerformingSkillsEvent(System.currentTimeMillis(), "info", this);
public void performingSkills() {
isWakeUp = true;
for (Observe observe : observeList) {
observe.actionClapping(performingSkillsEvent);
}
}
}
public class Main {
public static void main(String[] args) {
Performer performer = new Performer();
performer.addListener(new Audience1());
performer.addListener(new Audience2());
performer.performingSkills();
}
}
标签:Performer,void,观察者,模式,class,PerformingSkillsEvent,public
From: https://www.cnblogs.com/handsometaoa/p/17037510.html