zoukankan      html  css  js  c++  java
  • 设计模式之观察者模式(observer pattern)

    观察者模式主要用于处理对象间的一对多的关系,是一种对象行为模式。该模式的实际应用场景比较容易确认,当一个对象状态发生变化时,所有该对象的关注者均能收到状态变化通知,以进行相应的处理。
    本文希望通过简单的介绍和分析,能让读者对观察者模式有一个简单直观的认识和感知,以便在实际开发中根据需要灵活运用。

    1. 目的

    建立对象间一对多的关联关系,并能使一个对象的变化被所有关联对象感知。

    2. 动机

    建立一套低耦合的消息触发机制。

    3. 优缺点

    优点:

    1. 被观察者和观察者之间是抽象耦合的;
    2. 耦合度较低,两者之间的关联仅仅在于消息的通知;
    3. 被观察者无需关心他的观察者;
    4. 支持广播通信;

    缺点:

    1. 观察者只知道被观察对象发生了变化,但不知变化的过程和缘由;
    2. 观察者同时也可能是被观察者,消息传递的链路可能会过长,完成所有通知花费时间较多;
    3. 如果观察者和被观察者之间产生循环依赖,或者消息传递链路形成闭环,会导致无限循环;

    4. 应用场景

    • 需要在系统中建立一个单项广播的触发机制;
    • 系统中某个对象的行为会影响若干其他对象;
    • 对象之间的关联关系可以在运行时动态的建立与撤销;
    • 对象之间的关联关系呈现出一种树状结构;

    5.  原理

    下面是GoF介绍的典型的类观察者模式的UML类图:

    Subject:

     抽象被观察者,仅提供注册和删除观察者对象的接口声明。

    ConcreteSubject:

     具体被观察者对象,该对象中收集了所有需要被通知的观察者,并可以动态的增删集合中的观察者。当其状态发生变化时会通知所有观察者对象。

    Observer:

     抽象观察者,为所有观察者定义获得通知的统一接口;

    ConcreteObserver:

     观察者对象,其关注对象为Subject,能接受Subject变化时发出的通知并更新自身状态。

    6.实现

    接下来先将上面的UML类图转换为具体的代码,然后在举一个具体的例子来看一下其应用。

    抽象被观察者类:Subject

    public interface Subject {
        public void setState(int state);
        public int getState();
        public void attach(Observer obs);
        public void detach(Observer obs);
        public void notify(String msg);
    }

     抽象观察者类:Observer

    public interface Observer {
        public void update(String msg);
    }

    具体被观察者类:ConcreteSubject

    public class ConcreteSubject implements Subject {
        
        private List<Observer> observerList = new ArrayList<Observer>();
        private int state;
    
        @Override
        public void setState(int state) {
            this.state = state;        
            notify("new state: " + state);
        }
    
        @Override
        public int getState() {
            // TODO Auto-generated method stub
            return 0;
        }
        
        @Override
        public void attach(Observer obs) {
            // TODO Auto-generated method stub
            observerList.add(obs);
        }
    
        @Override
        public void detach(Observer obs) {
            // TODO Auto-generated method stub
            observerList.remove(obs);
        }
    
        @Override
        public void notify(String msg) {
            // TODO Auto-generated method stub
            for (Observer obs: observerList) {
                obs.update(msg);
            }
        }
    }

    具体观察者类:ConcreteObserver

    public class ConcreteObserver implements Observer {
    
        @Override
        public void update(String msg) {
            // TODO Auto-generated method stub
            System.out.println("ConcreteObserver receive notify msg: " + msg);
        }
    
    }

    演示:

    public class Demo {
        public static void main(String[] args) {
            ConcreteObserver obs = new ConcreteObserver();
            ConcreteSubject sub = new ConcreteSubject();
            sub.attach(obs);
            sub.setState(666);
            sub.notify("just test subject notify function!");
        }
    }

    结果:

    ConcreteObserver receive notify msg: new state: 666
    ConcreteObserver receive notify msg: just test subject notify function!

    7.实例

    我们以一个更加实际的例子——商品价格的变动来体会一下观察者模式的用途。

    在网上购物的时候,商品一般都有一个价格变动通知,前提是我们关注了该商品。

    这里我们稍微变通一下,只有当关注的商品价格下降,且低于用户期望购买价格的时候,才会给用户发送一条商品降价的短信通知。

    下面分别定义每个类:

    产品抽象类:Product

    public interface Product {
        public void setPrice(int price);
        public int getPrice();
        public void follow(User user);
        public void unfollow(User user);
        public void notifyLowPrice();
    }

    用户抽象类:User

    public interface User {
        public boolean isExpectedPrice(int price);
        public void shortMSG(String msg);
    }

    商品笔记本电脑:Laptop

    public class Laptop implements Product {
        
        private List<User> followList = new ArrayList<User>();
        private int curPrice;
    
        @Override
        public void setPrice(int price) {
            curPrice = price;
            System.out.println("set laptop price: " + price);
            notifyLowPrice();
        }
    
        @Override
        public int getPrice() {
            return curPrice;
        }
        
        @Override
        public void follow(User user) {
            followList.add(user);
        }
    
        @Override
        public void unfollow(User user) {
            followList.remove(user);
        }
    
        @Override
        public void notifyLowPrice() {
            String msg = "" + curPrice;
            for (User user: followList) {
                if (user.isExpectedPrice(curPrice)) {
                    user.shortMSG(msg);
                }
            }
        }
    }

    关注笔记本电脑用户类:LaptopBuyer

    public class LaptopBuyer implements User {
        private int expectedPrice;
        private String userName;
        public LaptopBuyer(String userName, int expectedPrice) {
            this.userName = userName;
            this.expectedPrice = expectedPrice;
        }
    
        @Override
        public boolean isExpectedPrice(int curPrice) {
            // TODO Auto-generated method stub
            return curPrice <= expectedPrice;
        }
    
        @Override
        public void shortMSG(String msg) {
            // TODO Auto-generated method stub
            System.out.println("Your follow product have a low price: " + msg + " TO:" + userName);
        }
    
    }

    演示:

    public class Demo {
        public static void main(String[] args) {
            LaptopBuyer Alice = new LaptopBuyer("Alice", 6000);
            LaptopBuyer Jack = new LaptopBuyer("Jack", 6500);
            Laptop laptop = new Laptop();
            laptop.follow(Alice);
            laptop.follow(Jack);
            laptop.setPrice(7000);
            laptop.setPrice(6500);
            laptop.setPrice(6000);
            laptop.unfollow(Jack);
            laptop.setPrice(5999);
            laptop.setPrice(6099);
        }
    }

    结果:

    set laptop price: 7000
    set laptop price: 6500
    Your follow product have a low price: 6500 TO:Jack
    set laptop price: 6000
    Your follow product have a low price: 6000 TO:Alice
    Your follow product have a low price: 6000 TO:Jack
    set laptop price: 5999
    Your follow product have a low price: 5999 TO:Alice
    set laptop price: 6099

    上面的这个例子是一个能够很好地解释观察者模式的一个实际用途。

    8. 总结

    相比较与观察者模式,我们或许有许多获取另外一个对象状态的方式,比如,常见的轮询方式,或者仅仅在需要的时候去查一下对方的状态等,不过观察者模式有其特殊的用途,而且更加灵活。

    该模式原理比较简单直接,但是实际使用过程中需要考虑一些细节问题:

    • 何时通知?
    • 有谁触发通知?
    • 观察者是关注状态变化的次数还是最终的状态?
    • 如果消息通知被阻塞,应该怎么办?
    • 是否可以改为异步消息通知?

    上面这些都是实际使用时应该考虑的。考虑清楚这些细节才能更灵活的应用该模式解决实际问题。

    参考:

    GoF《Design Patterns: Elements of Reusable Object-Oriented Software》

    https://www.runoob.com/design-pattern/observer-pattern.html

  • 相关阅读:
    BZOJ#2820. YY的GCD
    BZOJ#3771. Triple(FFT+生成函数)
    BZOJ#4503. 两个串
    HDU#4609. 3-idiots
    UVA 12298 Super Poker II(FFT+母函数)
    HDU#1402. A×B
    树状数组的区间修改与区间查修
    COCI. DIFERENCIJA(序列处理中的小技巧)
    dt.select默认不区分大小写
    缓存,缓存算法和缓存框架
  • 原文地址:https://www.cnblogs.com/yssjun/p/11107038.html
Copyright © 2011-2022 走看看