既然来了简书,总要留下点什么.......
定义
当一个对象的状态发生改变,与它相关联的部分对象的状态同时也会发生改变。
举例子:
我MY订阅了Android进阶之旅的公众号,每更新一篇文章都会及时的通知我。
角色划分
被观察者:公众号
具体的被观察者:Android进阶之旅微信公众号
观察者:用户
具体的观察者:订阅Android进阶之旅微信公众号的用户MY
伪代码1
//观察者
public interface IWXUser {
void push(String article);
}
public class WXRealUser implements IWXUser{
private String name;
public WXRealUser(String name) {
this.name = name;
}
@Override
public void push(String article) {
System.out.println(name+"收到文章"+article+"的推送");
}
}
//被观察者
public class WXPublicObservable {
public List<IWXUser> mList;
public WXPublicObservable() {
this.mList = new ArrayList<>();
}
//订阅
public void register(IWXUser user){
mList.add(user);
}
//取消订阅
public void unregister(IWXUser user){
mList.remove(user);
}
public void update(String article){
for (IWXUser user : mList) {
user.push(article);
}
}
}
public class WXRealObservable extends WXPublicObservable {
private String article;
public String getArticle(){
return article;
}
public void setArticle(String article){
this.article = article;
//推送
update(article);
}
}
//调用
public class Client {
public static void main(String[] args) {
WXRealObservable wxRealObservable = new WXRealObservable();
WXRealUser wxRealUser = new WXRealUser("fxy");
wxRealObservable.register(wxRealUser);
wxRealObservable.setArticle("志气");
}
}
推拉模式
订阅号主动推送一篇文章给我们(推模式)(伪代码见上方代码);
用户主动拉取一篇历史文章查看(拉模式);
推拉模式的结合。
伪代码2
public interface PullIWXUser {
void pull(PullWXPublicObservable observable);
}
public class PullWXRealUser implements PullIWXUser {
private String name;
public PullWXRealUser(String name) {
this.name = name;
}
@Override
public void pull(PullWXPublicObservable observable) {
PullWXRealObservable realObservable = (PullWXRealObservable) observable;
System.out.println(name+"拉取了文章"+realObservable.getArticle());
}
}
public class PullWXPublicObservable {
public List<PullIWXUser> mList;
public PullWXPublicObservable() {
this.mList = new ArrayList<>();
}
//订阅
public void register(PullIWXUser user){
mList.add(user);
}
//取消订阅
public void unregister(PullIWXUser user){
mList.remove(user);
}
public void update(String article){
for (PullIWXUser user : mList) {
user.pull(this);
}
}
}
其余的代码不变
区别:
拉模式:主动;
推模式:被动。
Java中自带的观察者(推拉)模式
java.util.Observable
伪代码3:
//被观察者
public class WXRealObservable extends Observable {
private String article;
public String getArticle(){
return article;
}
public void setArticle(String article){
this.article = article;
setChanged();
notifyObservers(article);
}
}
//观察者
public class WXRealUser implements Observer {
private String name;
public WXRealUser(String name) {
this.name = name;
}
@Override
public void update(Observable o, Object arg) {
System.out.println(name+"收到文章"+arg+"的推送");
WXRealObservable wxRealObservable = (WXRealObservable) o;
System.out.println(name+"主动拉取了一篇文章"+wxRealObservable.getArticle());
}
}
//调用
public class Client {
public static void main(String[] args) {
WXRealObservable wxRealObservable = new WXRealObservable();
WXRealUser wxRealUser = new WXRealUser("fxy");
WXRealUser wx2RealUser = new WXRealUser("my");
wxRealObservable.addObserver(wxRealUser);
wxRealObservable.addObserver(wx2RealUser);
wxRealObservable.setArticle("争气");
wxRealObservable.deleteObserver(wx2RealUser);
wxRealObservable.setArticle("不放弃");
}
}