Giter VIP home page Giter VIP logo

headfirstjava's Introduction

设计模式

下载地址

书中源码地址

网络链接

网络链接2

观察者模式

定义
  1. 针对观察者与被观察者分别定义接口,有利于分别进行扩展。
  2. 重点就在于被观察者的实现中:
    • 定义观察者集合,并定义针对集合的添加、删除操作,用于添加、删除订阅者(观察者)
    • 定义通知方法,用于将新情况通知给观察者用户(订阅者用户)
  3. 观察者中需要有一个接口观察者通知的方法。
例子:

例子:

警察在找到嫌犯的时候,为了找到幕后主使,一般都会蹲点监察,这里我有三名便衣警察来蹲点监察2名嫌犯,三名便衣分别是:张昊天、石破天、赵日天,两名嫌犯是:大熊与黑狗

定义一个嫌犯接口(被观察者)

定义一个警察接口(观察者)

在嫌犯接口中定义三个方法

​ 嫌犯接口定义集合,集合中存储警察接口。

​ 1 对上述集合的添加

​ 2 对上出集合的删除

​ 3 定义通知(通知观察者)方法

警察接口类中定义通知方法,接收嫌犯通知。

专有名字

主题

观察者,观察者依赖主题,只要主题有变化,观察者就会被通知。

headfirstjava's People

Contributors

onemoretrees avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.