package observerpattern;
import java.util.arraylist;
import java.util.list;
/**
* created by jy on 2018/11/28.
*/
public abstract class subject {
//维护一个所有观察者集合
private list<observer> list = new arraylist<>();
//新注册一个观察者
public void attach(observer observer){
list.add(observer);
system.out.println("新注册一个观察者");
}
//删除一个已注册的观察者
public void detach(observer observer){
list.remove(observer);
system.out.println("删除一个已注册的观察者");
}
//通知所有已经注册的观察者
public void notifyobservers(string state){
for (int i = 0; i < list.size(); i++) {
list.get(i).update(state);
}
}
}
(2)observer.java
package observerpattern;
/**
* created by jy on 2018/11/28.
*/
public interface observer {
// 抽象出的更新行为
public void update(string state);
}
(3)concretesubject.java
package observerpattern;
/**
* created by jy on 2018/11/28.
*/
public class concretesubject extends subject{
//真实主题内维护一个状态
private string state;
public string getstate() {
return state;
}
public void change(string state){
this.state = state;
system.out.println("真实主题状态变化为:"+state);
this.notifyobservers(state);
}
}
(4)concreteobserver.java
package observerpattern;
/**
* created by jy on 2018/11/28.
*/
public class concreteobserver implements observer {
//具体观察者的状态
private string observerstate;
@override
public void update(string state) {
//这里可以根据传递过来的主题的状态作出相应的业务
observerstate = state;
system.out.println("观察者的状态跟着变化为:"+observerstate);
}
}
(5)main.java
package observerpattern;
/**
* created by jy on 2018/11/28.
*/
public class main {
public static void main(string[] args) {
//真实主题
concretesubject concretesubject = new concretesubject();
//真实观察者
concreteobserver concreteobserver = new concreteobserver();
//观察者先注册
concretesubject.attach(concreteobserver);
//改变真实主题状态
concretesubject.change("2");
}
}
@override
public void addapplicationlistener(applicationlistener<?> listener) {
assert.notnull(listener, "applicationlistener must not be null");
//listener传入持有的一个的applicationeventmulticaster类中
if (this.applicationeventmulticaster != null) {
this.applicationeventmulticaster.addapplicationlistener(listener);
}
this.applicationlisteners.add(listener);
}
//省略部分代码
protected void registerlisteners() {
// register statically specified listeners first.
for (applicationlistener<?> listener : getapplicationlisteners()) {
getapplicationeventmulticaster().addapplicationlistener(listener);
}
// do not initialize factorybeans here: we need to leave all regular beans
// uninitialized to let post-processors apply to them!
string[] listenerbeannames = getbeannamesfortype(applicationlistener.class, true, false);
for (string listenerbeanname : listenerbeannames) {
getapplicationeventmulticaster().addapplicationlistenerbean(listenerbeanname);
}
// publish early application events now that we finally have a multicaster...
set<applicationevent> earlyeventstoprocess = this.earlyapplicationevents;
this.earlyapplicationevents = null;
if (earlyeventstoprocess != null) {
for (applicationevent earlyevent : earlyeventstoprocess) {
getapplicationeventmulticaster().multicastevent(earlyevent);
}
}
}
在abstractapplicationcontext中publishevent:
protected void publishevent(object event, @nullable resolvabletype eventtype) {
//.....
// multicast right now if possible - or lazily once the multicaster is initialized
if (this.earlyapplicationevents != null) {
this.earlyapplicationevents.add(applicationevent);
}
else {
getapplicationeventmulticaster().multicastevent(applicationevent, eventtype); //事件广播
//....
}