观察者模式
介绍
观察者模式又称为源收听模式,观察者与被观察者之间是一对多的关系,简而言之,观察者模式就是,当被观察者发生变动,系统会主动他通知观察者,从而做一些处理。
核心思想
场景
接下来实现一个场景:演员与观众,演员刀哥在台上表演,只要刀哥一表演到绝活,台下观众便会发出雷鸣般的掌声。
事件抽象类
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();
}
}
观察者模式在java中使用
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
class Source {
/**
* 被观察者 变量
*/
private String name;
private PropertyChangeSupport listeners = new PropertyChangeSupport(this);
public Source() {
this.name = "my name";
}
public String getName() {
return this.name;
}
public void setName(String newValue) {
String oldValue = this.name;
this.name = newValue;
//发布监听事件
firePropertyChange("name", oldValue, newValue);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
listeners.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
listeners.removePropertyChangeListener(listener);
}
protected void firePropertyChange(String prop, Object oldValue, Object newValue) {
listeners.firePropertyChange(prop, oldValue, newValue);
}
}
class Listener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("OldValue:" + evt.getOldValue() +
"| NewValue:" + evt.getNewValue() +
"| tPropertyName:" + evt.getPropertyName());
}
}
public class Main {
public static void main(String[] args) {
// 创建被观察者
Source source = new Source();
// 创建观察者
Listener listener = new Listener();
source.addPropertyChangeListener(listener);
source.setName("new Name1");
source.setName("new Name2");
source.removePropertyChangeListener(listener);
source.setName("new Name3");
source.addPropertyChangeListener(listener);
source.setName("new Name4");
}
}
本文来自博客园,作者:帅气的涛啊,转载请注明原文链接:https://www.cnblogs.com/handsometaoa/p/17037510.html