Android—用最简单的案例 Demo 带你分析解读:Android 观察者模式
package com.oop.demo5;
import java.util.ArrayList;import java.util.List;
/**
华为服务器对象
@author Liudeli
*/public class HUAWEIService extends Service {
private List<IPhone> listPhone = new ArrayList<IPhone>();
@Overridepublic void add(IPhone iPhone) {listPhone.add(iPhone);}
@Overridepublic void sendUpdateMsg() {for (IPhone iphone : listPhone) {iphone.update();}}
}
package com.oop.demo5;
import java.util.ArrayList;import java.util.List;
/**
戴尔服务器对象
@author Liudeli
*/public class DELLService extends Service {
private List<IPhone> listPhone = new ArrayList<IPhone>();
@Overridepublic void add(IPhone iPhone) {listPhone.add(iPhone);}
@Overridepublic void sendUpdateMsg() {for (IPhone iphone : listPhone) {iphone.update();}}
}
package com.oop.demo5;
/**
更新手机的规范
@author Liudeli
*/public interface IPhone {
/**
更新所有手机的行为标准规范*/public void update();
}
package com.oop.demo5;
/**
魅族手机对象
@author Liudeli
*/public class MeizuPhone implements IPhone {
public void update() {System.out.println("魅族手机需要去更新 QQ 了哦...");}
}
package com.oop.demo5;
/**
小米手机对象
@author Liudeli
*/public class MIPhone implements IPhone {
public void update() {System.out.println("小米手机更新 QQ 了...");}
}
package com.oop.demo5;
/**
三星手机对象
@author Liudeli
*/public class SamsungPhone implements IPhone {
public void update() {System.out.println("三星手机该更新 QQ 啦...");}
}
package com.oop.demo5;
/**
步步高手机对象
@author Liudeli
*/public class VivoPhone implements IPhone {
public void update() {System.out.println("步步高手机需要去更新手机 QQ 了哦...");}
}
/**
测试端程序
@author Liudeli
*/public class Main {
public static void main(String[] args) {
// 使用戴尔服务器,来更新手机 QQService service = new DELLService();
// 增加观察者(增加需要更新的手机对象)service.add(new MIPhone());service.add(new MeizuPhone());service.add(new SamsungPhone());service.add(new VivoPhone());
// 给手机发出通知 service.sendUpdateMsg();
System.out.println("省略...");}
}
如果某天,戴尔服务器出问题了,自动切换到备用服务器 华为服务器:
// 使用华为服务器,来更新手机 QQService service = new HUAWEIService();
案例二:(通知同事)
package com.oop.demo3;
/**
定义观察者抽象类
@author Liudeli
*/public abstract class Observer {
private String name;private ISecretary secretary;
public Observer(String name, ISecretary secretary) {this.name = name;this.secretary = secretary;}
/**
更新*/public abstract void update();
}
package com.oop.demo3;
/**
不能依赖细节,细节应该依赖抽象,老板,和秘书,和其他秘书都是细节,抽象则是这些细节所定义的规范
@author Liudeli
*/public interface ISecretary {
/**
增加
@param stockObserver*/public void add(Observ
er observer);
/**
通知*/public void notice();
/**
前台状态*/
public String getAction();
public void setAction(String action);
}
package com.oop.demo3;
/**
定义股票观察员对象
@author Liudeli
*/public class StockObserver extends Observer{
private String name;private ISecretary secretary;
public StockObserver(String name, ISecretary secretary) {super(name, secretary);this.name = name;this.secretary = secretary;}
@Overridepublic void update() {System.out.println(secretary.getAction() + name + " 请你关闭股票行情,继续工作");}
}
package com.oop.demo3;
/**
定义看 NBA 的同事
@author Liudeli
*/public class NBAObserver extends Observer {
private String name;private ISecretary secretary;
public NBAObserver(String name, ISecretary secretary) {super(name, secretary);this.name = name;this.secretary = secretary;}
@Overridepublic void update() {System.out.println(secretary.getAction() + name + " 请你关闭 NBA 视频画面,继续工作");}
}
package com.oop.demo3;
import java.util.ArrayList;import java.util.List;
/**
前台秘书类
@author Liudeli
*/public class Secretary implements ISecretary{
/**
定义同事集合*/private List<Observer> list = new ArrayList<Observer>();
/**
增加
@param stockObserver*/public void add(Observer observer) {list.add(observer);}
/**
通知*/public void notice() {
for (Observer observer : list) {observer.update();}}
/**
前台状态*/private String action;
public String getAction() {return this.action;}
public void setAction(String action) {this.action = action;}
}
package com.oop.demo3;
import java.util.ArrayList;import java.util.List;
/**
老板对象
@author Liudeli
*/public class Boss implements ISecretary {
/**
定义同事集合(具体抽象)*/private List<Observer> list = new ArrayList<Observer>();
/**
增加
@param stockObserver*/public void add(Observer observer) {list.add(observer);}
/**
通知*/public void notice() {
for (Observer observer : list) {observer.update();}}
/**
前台状态*/private String action;
public String getAction() {
评论