观察者模式
2018-11-05 本文已影响0人
要学的东西太多了
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己。Android
中我们常用到的监听事件也属于观察者模式,以及RX
系列都是这种模式。分为推模型和拉模型,推模型是以参数的方式将更新的部分数据或全部数据传递给观察者,一旦观察者需要的数据有变动,观察者和被观察者都要去改方法,不易于维护拓展;而拉模型是将被观察者本身提供查询数据的方法,更新时将自身传递给观察者,观察者按需要去取数据,比较灵活和便于拓展。
示例如下:
推模型
public class Observer {
public static void main(String[] a){
Observer observer=new Observer();
Product product=observer.new Product("NIKE AIR",4000);
IPurchaser 张三 = observer.new NIKEFans("张三");
IPurchaser 李四 = observer.new NIKEFans("李四");
IPurchaser 王五 = observer.new NIKEFans("王五");
IPurchaser 麻子 = observer.new NIKEFans("麻子");
IStore Nike = observer.new NIKE();
Nike.registerPurchaser(张三);
Nike.registerPurchaser(李四);
Nike.registerPurchaser(王五);
Nike.registerPurchaser(麻子);
Nike.ArrivalNewProduct(product);
}
class Product{
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Product{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
interface IStore{
void registerPurchaser(IPurchaser iPurchaser);
void unRegisterPurchaser(IPurchaser iPurchaser);
void ArrivalNewProduct(Product product);
void notifyNewProduct(Product product);
}
class NIKE implements IStore{
private List<Product> products=new ArrayList<>();
private List<IPurchaser> purchasers=new ArrayList<>();
@Override
public void ArrivalNewProduct(Product product){
products.add(product);
notifyNewProduct(product);
}
@Override
public void registerPurchaser(IPurchaser iPurchaser) {
purchasers.add(iPurchaser);
}
@Override
public void unRegisterPurchaser(IPurchaser iPurchaser) {
purchasers.remove(iPurchaser);
}
@Override
public void notifyNewProduct(Product product) {
for(IPurchaser iPurchaser:purchasers){
iPurchaser.newProductArrivals(product);
}
}
}
interface IPurchaser{
void newProductArrivals(Product product);
}
class NIKEFans implements IPurchaser{
private String name;
public NIKEFans(String name) {
this.name = name;
}
@Override
public void newProductArrivals(Product product) {
System.out.println(name+"新货到了,快去买"+product.toString());
}
}
}
拉模型
public class Observer {
public static void main(String[] a){
Observer observer=new Observer();
Product product=observer.new Product("NIKE AIR",4000);
IPurchaser 张三 = observer.new NIKEFans("张三");
IPurchaser 李四 = observer.new NIKEFans("李四");
IPurchaser 王五 = observer.new NIKEFans("王五");
IPurchaser 麻子 = observer.new NIKEFans("麻子");
IStore Nike = observer.new NIKE();
Nike.registerPurchaser(张三);
Nike.registerPurchaser(李四);
Nike.registerPurchaser(王五);
Nike.registerPurchaser(麻子);
Nike.ArrivalNewProduct(product);
}
class Product{
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Product{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
interface IStore{
void registerPurchaser(IPurchaser iPurchaser);
void unRegisterPurchaser(IPurchaser iPurchaser);
void ArrivalNewProduct(Product product);
void notifyNewProduct();
}
class NIKE implements IStore{
private Product newProduct;
private List<IPurchaser> purchasers=new ArrayList<>();
public Product getNewProduct() {
return newProduct;
}
@Override
public void ArrivalNewProduct(Product product){
newProduct=product;
notifyNewProduct();
}
@Override
public void registerPurchaser(IPurchaser iPurchaser) {
purchasers.add(iPurchaser);
}
@Override
public void unRegisterPurchaser(IPurchaser iPurchaser) {
purchasers.remove(iPurchaser);
}
@Override
public void notifyNewProduct() {
for(IPurchaser iPurchaser:purchasers){
iPurchaser.newProductArrivals(this);
}
}
}
interface IPurchaser{
void newProductArrivals(IStore iStore);
}
class NIKEFans implements IPurchaser{
private String name;
public NIKEFans(String name) {
this.name = name;
}
@Override
public void newProductArrivals(IStore iStore) {
System.out.println(name+"新货到了,快去买"+((NIKE)iStore).getNewProduct().toString());
}
}
}