美文网首页
观察者模式

观察者模式

作者: keith666 | 来源:发表于2016-05-15 17:39 被阅读24次

Intent

  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • Encapsulate the core (or common or engine) components in a Subject abstraction, and the variable (or optional or user interface) components in an Observer hierarchy.
  • The "View" part of Model-View-Controller.

Structure

Observer by keith
  • 实现代码:
  • public class Observer {
        public static void main(String[] args){
            Feeder feeder=new Feeder();
            Observable ob1=new Cat();
            Observable ob2=new Dog();
            Observable ob3=new Sheep();
    
            //register the observable
            feeder.register(ob1);
            feeder.register(ob2);
            feeder.register(ob3);
    
            //change the msg
            feeder.changeMsg("time to eat");
        }
    }
    //observable interface
    interface Observable {
        // method to be called by subject
        void update(String msg);
    }
    //Cat
    class Cat implements Observable {
    
        @Override
        public void update(String msg) {
            System.out.println(getClass().getSimpleName() + " received: " + msg);
        }
    }
    //Dog
    class Dog implements Observable {
    
        @Override
        public void update(String msg) {
            System.out.println(getClass().getSimpleName() + " received: " + msg);
        }
    }
    //Sheep
    class Sheep implements Observable {
    
        @Override
        public void update(String msg) {
            System.out.println(getClass().getSimpleName() + " received: " + msg);
        }
    }
    //subject interface
    interface Subject {
        void register(Observable obs);
    
        void unregister(Observable obs);
    
        void notifyDataChanged(String msg);
    }
    class Feeder implements Subject {
    
        private List<Observable> observables;
    
        private String message;
    
        public Feeder() {
            this.observables = new ArrayList<>();
        }
    
        public void changeMsg(String msg) {
            message = msg;
            notifyDataChanged(msg);
        }
    
        @Override
        public void register(Observable obs) {
            observables.add(obs);
        }
    
        @Override
        public void unregister(Observable obs) {
            observables.remove(obs);
        }
    
        @Override
        public void notifyDataChanged(String msg) {
            for (Observable obs : observables) {
                obs.update(msg);
            }
        }
    }
    
    1. 运行结果:
    Cat received: time to eat
    Dog received: time to eat
    Sheep received: time to eat
    

    Refenrence

    1. Design Patterns
    2. 设计模式

    相关文章

    • 11.9设计模式-观察者模式-详解

      设计模式-观察者模式 观察者模式详解 观察者模式在android中的实际运用 1.观察者模式详解 2.观察者模式在...

    • RxJava基础—观察者模式

      设计模式-观察者模式 观察者模式:观察者模式(有时又被称为发布(publish )-订阅(Subscribe)模式...

    • 前端面试考点之手写系列

      1、观察者模式 观察者模式(基于发布订阅模式) 有观察者,也有被观察者。 观察者需要放到被观察者列表中,被观察者的...

    • RxJava 原理篇

      一、框架思想 观察者模式观察者自下而上注入被观察者被观察者自上而下发射事件观察者模式 装饰器模式自上而下,被观察者...

    • 观察者模式

      观察者模式概念 观察者模式是对象的行为模式,又叫作发布-订阅(publish/subscrible)模式。 观察者...

    • 设计模式-观察者模式

      观察者模式介绍 观察者模式定义 观察者模式(又被称为发布-订阅(Publish/Subscribe)模式,属于行为...

    • 观察者模式

      观察者模式 观察者模式的定义 观察者模式(Observer Pattern)也叫做发布订阅模式(Publish/s...

    • iOS设计模式之观察者模式

      观察者模式 1、什么是观察者模式 观察者模式有时又被称为发布(publish)-订阅(Subscribe)模式、模...

    • 观察者模式和发布订阅模式区别

      观察者模式 所谓观察者模式,其实就是为了实现松耦合(loosely coupled)。 在观察者模式中,观察者需要...

    • RxJava(二)

      一、观察者模式 1.1、传统的观察者模式 1.2、RxJava 的观察者模式 区别传统的观察者模式是一个 Obse...

    网友评论

        本文标题:观察者模式

        本文链接:https://www.haomeiwen.com/subject/cabvrttx.html