vue2从数据变化到视图变化发布订阅模式详解

目录
  • 引言
  • 一、发布订阅者模式的特点
  • 二、vue中的发布订阅者模式
    • 1、dep
    • 2、Object.defineProperty
    • 3、watcher
    • 4、dep.depend
    • 5、dep.notify
    • 6、订阅者取消订阅
  • 小结

引言

发布订阅者模式是最常见的模式之一,它是一种一对多的对应关系,当一个对象发生变化时会通知依赖他的对象,接受到通知的对象会根据情况执行自己的行为。

假设有财经报纸送报员financialDep,有报纸阅读爱好者a,b,c,那么a,b,c想订报纸就告诉financialDep,financialDep依次记录a,b,c这三个人的家庭地址,次日,送报员一大早把报纸送到a,b,c家门口的邮箱中,a,b,c收到报纸后都会认认真真的打开阅读。随着时间的推移,会有以下几种场景:

  • 有新的订阅者加入: 有一天d也想订报纸了,那么找到financialDep,financialDep把d的家庭地址记录到a,b,c的后面,次日,为a,b,c,d分别送报纸。
  • 有订阅者退出了:有一天a要去旅游了,提前给送报员financialDep打电话取消了订阅,如果不取消的话,积攒的报纸就会溢出小邮箱。
  • 有新的报社开业:有一天镇子又开了家体育类的报馆,送报员是sportDep,b和d也是球类爱好者,于是在sportDep那里做了登记,sportDep的记录中就有了b和d。
    从上面的例子中可以看出,刚开始送报员financialDep的记录中有a,b和c,先是d加进来后来是a离开,最终financialDep的记录中有b,c和d。体育类报馆开张的时候,b和d也订阅了报纸,sportDep的记录中就有了b和d。我们发现,c只订阅了财经类报刊,而b和d既订阅了财经类的报纸也定了财经类的报刊。

一、发布订阅者模式的特点

从以上例子可以发现特点:

  • 发布者可以支持订阅者的加入
  • 发布者可以支持订阅者的删除
  • 一个发布者可以有多个订阅者,一个订阅者也可以订阅多个发布者的消息那可能会有疑问,有没有可能会有发布者的删除,答案是会,但是此时,发布者已消失,订阅者再也不会收到消息,也就不会与当前发布者相关的消息诱发的行为。好比体育类报馆关停了(发布者删除)那么b和d在也不会收到体育类报纸(消息),也就不会再阅读体育类报纸(行为)。

二、vue中的发布订阅者模式

以上的例子基本就是vue中发布订阅者的大体概况,vue中的发布者是啥时候定义的?
new Vue实例化的过程中会执行this._init的初始化方法,_init方法中有方法initState

export function initState (vm: Component) {
  // ...
  if (opts.data) {
    initData(vm)
  } else {
    observe(vm._data = {}, true /* asRootData */)
  }
  //...
}

首先看initData对于data的初始化:

function initData (vm: Component) {
  let data = vm.$options.data
  data = vm._data = typeof data === 'function'
    ? getData(data, vm)
    : data || {}
  if (!isPlainObject(data)) {
    data = {}
    process.env.NODE_ENV !== 'production' && warn(
      'data functions should return an object:\n' +
      'https://vuejs.org/v2/guide/components.html#data-Must-Be-a-Function',
      vm
    )
  }
  // proxy data on instance
  const keys = Object.keys(data)
  const props = vm.$options.props
  const methods = vm.$options.methods
  let i = keys.length
  while (i--) {
    const key = keys[i]
    if (process.env.NODE_ENV !== 'production') {
      if (methods && hasOwn(methods, key)) {
        warn(
          `Method "${key}" has already been defined as a data property.`,
          vm
        )
      }
    }
    if (props && hasOwn(props, key)) {
      process.env.NODE_ENV !== 'production' && warn(
        `The data property "${key}" is already declared as a prop. ` +
        `Use prop default value instead.`,
        vm
      )
    } else if (!isReserved(key)) {
      proxy(vm, `_data`, key)
    }
  }
  // observe data
  observe(data, true /* asRootData */)
}

这里首先获取data,如果data是函数又会执行getData方法。然后,获取methodsprops中的key值,如果已经定义过则在开发环境进行控制台警告。其中,proxy的目的是让访问this[key]相当于访问this._data[key]。最后,对数据进行响应式处理 observe(data, true /* asRootData */)

/**
 * Attempt to create an observer instance for a value,
 * returns the new observer if successfully observed,
 * or the existing observer if the value already has one.
 */
export function observe (value: any, asRootData: ?boolean): Observer | void {
  if (!isObject(value) || value instanceof VNode) {
    return
  }
  let ob: Observer | void
  if (hasOwn(value, '__ob__') && value.__ob__ instanceof Observer) {
    ob = value.__ob__
  } else if (
    shouldObserve &&
    !isServerRendering() &&
    (Array.isArray(value) || isPlainObject(value)) &&
    Object.isExtensible(value) &&
    !value._isVue
  ) {
    ob = new Observer(value)
  }
  if (asRootData && ob) {
    ob.vmCount++
  }
  return ob
}

如果不是对象或者当前值是VNode的实例直接返回。如果当前当前值上有属性__ob__并且value.__ob__Observer的实例,那么说明该值已经被响应式处理过,直接将value.__ob__赋值给ob并在最后返回即可。如果满足else if中的条件,则可执行ob = new Observer(value):

/**
 * Observer class that is attached to each observed
 * object. Once attached, the observer converts the target
 * object's property keys into getter/setters that
 * collect dependencies and dispatch updates.
 */
export class Observer {
  value: any;
  dep: Dep;
  vmCount: number; // number of vms that have this object as root $data
  constructor (value: any) {
    this.value = value
    this.dep = new Dep()
    this.vmCount = 0
    def(value, '__ob__', this)
    if (Array.isArray(value)) {
      if (hasProto) {
        protoAugment(value, arrayMethods)
      } else {
        copyAugment(value, arrayMethods, arrayKeys)
      }
      this.observeArray(value)
    } else {
      this.walk(value)
    }
  }
  /**
   * Walk through all properties and convert them into
   * getter/setters. This method should only be called when
   * value type is Object.
   */
  walk (obj: Object) {
    const keys = Object.keys(obj)
    for (let i = 0; i < keys.length; i++) {
      defineReactive(obj, keys[i])
    }
  }
  /**
   * Observe a list of Array items.
   */
  observeArray (items: Array<any>) {
    for (let i = 0, l = items.length; i < l; i++) {
      observe(items[i])
    }
  }
}

Observer是构造函数,通过对value是否是数组的判断,分别执行observeArraywalkobserveArray会对数组中的元素执行observe(items[i]),即通过递归的方式对value树进行深度遍历,递归的最后都会执行到walk方法。再看walk中的defineReactive(obj, keys[i])方法:

/**
 * Define a reactive property on an Object.
 */
export function defineReactive (
  obj: Object,
  key: string,
  val: any,
  customSetter?: ?Function,
  shallow?: boolean
) {
  const dep = new Dep()
  const property = Object.getOwnPropertyDescriptor(obj, key)
  if (property && property.configurable === false) {
    return
  }
  // cater for pre-defined getter/setters
  const getter = property && property.get
  const setter = property && property.set
  if ((!getter || setter) && arguments.length === 2) {
    val = obj[key]
  }
  let childOb = !shallow && observe(val)
  Object.defineProperty(obj, key, {
    enumerable: true,
    configurable: true,
    get: function reactiveGetter () {
      const value = getter ? getter.call(obj) : val
      if (Dep.target) {
        dep.depend()
        if (childOb) {
          childOb.dep.depend()
          if (Array.isArray(value)) {
            dependArray(value)
          }
        }
      }
      return value
    },
    set: function reactiveSetter (newVal) {
      const value = getter ? getter.call(obj) : val
      /* eslint-disable no-self-compare */
      if (newVal === value || (newVal !== newVal && value !== value)) {
        return
      }
      /* eslint-enable no-self-compare */
      if (process.env.NODE_ENV !== 'production' && customSetter) {
        customSetter()
      }
      // #7981: for accessor properties without setter
      if (getter && !setter) return
      if (setter) {
        setter.call(obj, newVal)
      } else {
        val = newVal
      }
      childOb = !shallow && observe(newVal)
      dep.notify()
    }
  })
}

这里就是vue响应式原理、watcher订阅者收集、数据变化时发布者dep通知subs中订阅者watcher进行相应操作的主要流程,new Dep()实例化、Object.defineProperty方法、dep.depend()订阅者收集和dep.notify()是主要的功能。先看发布者Dep的实例化:

1、dep

import type Watcher from './watcher'
import { remove } from '../util/index'
import config from '../config'
let uid = 0
/**
 * A dep is an observable that can have multiple
 * directives subscribing to it.
 */
export default class Dep {
  static target: ?Watcher;
  id: number;
  subs: Array<Watcher>;
  constructor () {
    this.id = uid++
    this.subs = []
  }
  addSub (sub: Watcher) {
    this.subs.push(sub)
  }
  removeSub (sub: Watcher) {
    remove(this.subs, sub)
  }
  depend () {
    if (Dep.target) {
      Dep.target.addDep(this)
    }
  }
  notify () {
    // stabilize the subscriber list first
    const subs = this.subs.slice()
    if (process.env.NODE_ENV !== 'production' && !config.async) {
      // subs aren't sorted in scheduler if not running async
      // we need to sort them now to make sure they fire in correct
      // order
      subs.sort((a, b) => a.id - b.id)
    }
    for (let i = 0, l = subs.length; i < l; i++) {
      subs[i].update()
    }
  }
}
// The current target watcher being evaluated.
// This is globally unique because only one watcher
// can be evaluated at a time.
Dep.target = null
const targetStack = []
export function pushTarget (target: ?Watcher) {
  targetStack.push(target)
  Dep.target = target
}
export function popTarget () {
  targetStack.pop()
  Dep.target = targetStack[targetStack.length - 1]
}

这里的dep就相当于财经或者体育报馆,其中定义了属性idsubs,subs相当于送报员financialDep手中的笔记本,用来是用来记录订阅者的数组。发布者的消息如何发给订阅者,就需要借助Object.defineProperty:

2、Object.defineProperty

对于一个对象的属性进行访问或者设置的时候可以为其设置getset方法,在其中进行相应的操作,这也是vue响应式原理的本质,也是IE低版本浏览器不支持vue框架的原因,因为IE低版本浏览器不支持Object.defineProperty方法。

Object.defineProperty(obj, key, {
    enumerable: true,
    configurable: true,
    get: function reactiveGetter () {
      // 当访问属性的时候,进行订阅者的收集
    },
    set: function reactiveSetter (newVal) {
      // 当修改属性的时候,收到发布者消息的时候进行相应的操作
    }
  })

在vue中订阅者有computer watcher计算属性、watch watcher侦听器和render watcher渲染watcher。这里先介绍渲染watcher:

3、watcher

let uid = 0
/**
 * A watcher parses an expression, collects dependencies,
 * and fires callback when the expression value changes.
 * This is used for both the $watch() api and directives.
 */
export default class Watcher {
  vm: Component;
  expression: string;
  cb: Function;
  id: number;
  deep: boolean;
  user: boolean;
  lazy: boolean;
  sync: boolean;
  dirty: boolean;
  active: boolean;
  deps: Array<Dep>;
  newDeps: Array<Dep>;
  depIds: SimpleSet;
  newDepIds: SimpleSet;
  before: ?Function;
  getter: Function;
  value: any;
  constructor (
    vm: Component,
    expOrFn: string | Function,
    cb: Function,
    options?: ?Object,
    isRenderWatcher?: boolean
  ) {
    this.vm = vm
    if (isRenderWatcher) {
      vm._watcher = this
    }
    vm._watchers.push(this)
    // options
    if (options) {
      this.deep = !!options.deep
      this.user = !!options.user
      this.lazy = !!options.lazy
      this.sync = !!options.sync
      this.before = options.before
    } else {
      this.deep = this.user = this.lazy = this.sync = false
    }
    this.cb = cb
    this.id = ++uid // uid for batching
    this.active = true
    this.dirty = this.lazy // for lazy watchers
    this.deps = []
    this.newDeps = []
    this.depIds = new Set()
    this.newDepIds = new Set()
    this.expression = process.env.NODE_ENV !== 'production'
      ? expOrFn.toString()
      : ''
    // parse expression for getter
    if (typeof expOrFn === 'function') {
      this.getter = expOrFn
    } else {
      this.getter = parsePath(expOrFn)
      if (!this.getter) {
        this.getter = noop
        process.env.NODE_ENV !== 'production' && warn(
          `Failed watching path: "${expOrFn}" ` +
          'Watcher only accepts simple dot-delimited paths. ' +
          'For full control, use a function instead.',
          vm
        )
      }
    }
    this.value = this.lazy
      ? undefined
      : this.get()
  }
  /**
   * Evaluate the getter, and re-collect dependencies.
   */
  get () {
    pushTarget(this)
    let value
    const vm = this.vm
    try {
      value = this.getter.call(vm, vm)
    } catch (e) {
      if (this.user) {
        handleError(e, vm, `getter for watcher "${this.expression}"`)
      } else {
        throw e
      }
    } finally {
      // "touch" every property so they are all tracked as
      // dependencies for deep watching
      if (this.deep) {
        traverse(value)
      }
      popTarget()
      this.cleanupDeps()
    }
    return value
  }
  // watcher还有很多其他自定义方法,用的时候再列举
}

Watcher实例化的最后会执行this.value = this.lazy ? undefined : this.get()方法,默认this.lazy=false,满足条件执行Watcher实例的回调this.get()方法。 pushTarget(this)定义在dep.js文件中,为全局targetStack中推入当前订阅者,是一种栈的组织方式。Dep.target = target表示当前订阅者是正在计算中的订阅者,全局同一时间点有且只有一个。 然后执行value = this.getter.call(vm, vm),这里的this.getter就是

updateComponent = () => {
  vm._update(vm._render(), hydrating)
}

进行当前vue实例的渲染,在渲染过程中会创建vNode,进而访问数据data中的属性,进入到get方法中,触发dep.depend()

4、dep.depend

dep.depend()是在访问obj[key]的时候进行执行的,在渲染过程中Dep.target就是渲染watcher,条件满足,执行Dep.target.addDep(this),即执行watcher中的

    /**
   * Add a dependency to this directive.
   */
  addDep (dep: Dep) {
    const id = dep.id
    if (!this.newDepIds.has(id)) {
      this.newDepIds.add(id)
      this.newDeps.push(dep)
      if (!this.depIds.has(id)) {
        dep.addSub(this)
      }
    }
  }

newDepIdsdepIds分别表示当前订阅者依赖的当前发布者和旧发布者idSet集合,newDeps表示当前发布者实例的数组列表。首次渲染时this.newDepIds中不包含idthis.newDepIds添加了发布者的idthis.newDeps中添加了dep实例。同时,this.depIds中不包含id,继而执行到dep.addSub(this)

addSub (sub: Watcher) {
    this.subs.push(sub)
}

这个动作就表示订阅者watcher订阅了发布者dep发布的消息,当前发布者的subs数组中订阅者数量+1,等下次数据变化时发布者就通过dep.notify()的方式进行消息通知。

5、dep.notify

notify () {
    // stabilize the subscriber list first
    const subs = this.subs.slice()
    if (process.env.NODE_ENV !== 'production' && !config.async) {
      // subs aren't sorted in scheduler if not running async
      // we need to sort them now to make sure they fire in correct
      // order
      subs.sort((a, b) => a.id - b.id)
    }
    for (let i = 0, l = subs.length; i < l; i++) {
      subs[i].update()
    }
}

const subs = this.subs.slice()对订阅者进行浅拷贝,subs.sort((a, b) => a.id - b.id)按照订阅者的id进行排序,最后循环订阅者,订阅者触发update方法:

/**
   * Subscriber interface.
   * Will be called when a dependency changes.
   */
  update () {
    /* istanbul ignore else */
    if (this.lazy) {
      this.dirty = true
    } else if (this.sync) {
      this.run()
    } else {
      queueWatcher(this)
    }
  }

this.dirty表示计算属性,这里是falsethis.sync表示同步,这里是false,最后会走到queueWatcher(this):

/**
 * Push a watcher into the watcher queue.
 * Jobs with duplicate IDs will be skipped unless it's
 * pushed when the queue is being flushed.
 */
export function queueWatcher (watcher: Watcher) {
  const id = watcher.id
  if (has[id] == null) {
    has[id] = true
    if (!flushing) {
      queue.push(watcher)
    } else {
      // if already flushing, splice the watcher based on its id
      // if already past its id, it will be run next immediately.
      let i = queue.length - 1
      while (i > index && queue[i].id > watcher.id) {
        i--
      }
      queue.splice(i + 1, 0, watcher)
    }
    // queue the flush
    if (!waiting) {
      waiting = true
      if (process.env.NODE_ENV !== 'production' && !config.async) {
        flushSchedulerQueue()
        return
      }
      nextTick(flushSchedulerQueue)
    }
  }
}

这里未刷新状态flushing === false时会在队列queue中推入订阅者watcher,如果没有在等待状态waiting===false时执行nextTickflushSchedulerQueue的执行推入异步队列中,等待所有的同步操作执行完毕再去按照次序执行异步的flushSchedulerQueue。需要了解nextTick原理请移步:https://www.jb51.net/article/261842.htm

/**
 * Flush both queues and run the watchers.
 */
function flushSchedulerQueue () {
  currentFlushTimestamp = getNow()
  flushing = true
  let watcher, id
  // Sort queue before flush.
  // This ensures that:
  // 1. Components are updated from parent to child. (because parent is always
  //    created before the child)
  // 2. A component's user watchers are run before its render watcher (because
  //    user watchers are created before the render watcher)
  // 3. If a component is destroyed during a parent component's watcher run,
  //    its watchers can be skipped.
  queue.sort((a, b) => a.id - b.id)
  // do not cache length because more watchers might be pushed
  // as we run existing watchers
  for (index = 0; index < queue.length; index++) {
    watcher = queue[index]
    if (watcher.before) {
      watcher.before()
    }
    id = watcher.id
    has[id] = null
    watcher.run()
    // in dev build, check and stop circular updates.
    if (process.env.NODE_ENV !== 'production' && has[id] != null) {
      circular[id] = (circular[id] || 0) + 1
      if (circular[id] > MAX_UPDATE_COUNT) {
        warn(
          'You may have an infinite update loop ' + (
            watcher.user
              ? `in watcher with expression "${watcher.expression}"`
              : `in a component render function.`
          ),
          watcher.vm
        )
        break
      }
    }
  }
  // keep copies of post queues before resetting state
  const activatedQueue = activatedChildren.slice()
  const updatedQueue = queue.slice()
  resetSchedulerState()
  // call component updated and activated hooks
  callActivatedHooks(activatedQueue)
  callUpdatedHooks(updatedQueue)
  // devtool hook
  /* istanbul ignore if */
  if (devtools && config.devtools) {
    devtools.emit('flush')
  }
}
function callUpdatedHooks (queue) {
  let i = queue.length
  while (i--) {
    const watcher = queue[i]
    const vm = watcher.vm
    if (vm._watcher === watcher && vm._isMounted && !vm._isDestroyed) {
      callHook(vm, 'updated')
    }
  }
}

这里主要做了四件事:

  • 对队列queue进行排序
  • 遍历执行watcherrun方法
  • resetSchedulerState进行重置,清空queue,并且waiting = flushing = false进行状态重置
  • callUpdatedHooks执行callHook(vm, 'updated')生命周期钩子函数 这里的run是在Watcher的时候定义的:
/**
   * Scheduler job interface.
   * Will be called by the scheduler.
   */
  run () {
    if (this.active) {
      const value = this.get()
      if (
        value !== this.value ||
        // Deep watchers and watchers on Object/Arrays should fire even
        // when the value is the same, because the value may
        // have mutated.
        isObject(value) ||
        this.deep
      ) {
        // set new value
        const oldValue = this.value
        this.value = value
        if (this.user) {
          try {
            this.cb.call(this.vm, value, oldValue)
          } catch (e) {
            handleError(e, this.vm, `callback for watcher "${this.expression}"`)
          }
        } else {
          this.cb.call(this.vm, value, oldValue)
        }
      }
    }
  }

active默认为true,执行到const value = this.get()就开始了数据变化后的渲染的操作,好比订阅者收到报纸后认真读报一样。get方法中,value = this.getter.call(vm, vm)渲染执行完以后,会通过popTargettargetStack栈顶的元素移除,并且通过Dep.target = targetStack[targetStack.length - 1]修改当前执行的元素。最后执行this.cleanupDeps:

6、订阅者取消订阅

 /**
   * Clean up for dependency collection.
   */
  cleanupDeps () {
    let i = this.deps.length
    while (i--) {
      const dep = this.deps[i]
      if (!this.newDepIds.has(dep.id)) {
        dep.removeSub(this)
      }
    }
    let tmp = this.depIds
    this.depIds = this.newDepIds
    this.newDepIds = tmp
    this.newDepIds.clear()
    tmp = this.deps
    this.deps = this.newDeps
    this.newDeps = tmp
    this.newDeps.length = 0
  }

首先通过while的方式循环旧的this.deps发布者的数组,如果当前订阅者所依赖的发布者this.newDepIds中没有包含旧的发布者,那么,就让发布者在this.subs中移除订阅者,这样就不会让发布者dep进行额外的通知,这种额外的通知可能会引起未订阅者的行为(可能消耗内存资源或引起不必要的计算)。后面的逻辑就是让新旧发布者iddep进行交换,方便下次发布者发布消息后的清除操作。

小结

vue中的发布订阅者是在借助Object.defineProperty将数据变成响应式的过程中定义了dep,在get过程中dep对于订阅者的加入进行处理,在set修改数据的过程中dep通知订阅者进行相应的操作。

以上就是vue2从数据变化到视图变化发布订阅模式详解的详细内容,更多关于vue2数据视图变化发布订阅模式的资料请关注我们其它相关文章!

(0)

相关推荐

  • React Diff算法不采用Vue的双端对比原因详解

    目录 前言 React 官方的解析 Fiber 的结构 Fiber 链表的生成 React 的 Diff 算法 第一轮,常见情况的比对 第二轮,不常见的情况的比对 重点如何协调更新位置信息 小结 图文解释 React Diff 算法 最简单的 Diff 场景 复杂的 Diff 场景 Vue3 的 Diff 算法 第一轮,常见情况的比对 第二轮,复杂情况的比对 Vue2 的 Diff 算法 第一轮,简单情况的比对 第二轮,不常见的情况的比对 React.Vue3.Vue2 的 Diff 算法对比

  • vue中虚拟DOM与Diff算法知识精讲

    目录 前言 知识点: 虚拟DOM(Virtual DOM): 虚拟dom库 diff算法 snabbdom的核心 init函数 h函数 patch函数(核心) 题外话:diff算法简介 传统diff算法 snabbdom的diff算法优化 updateChildren(核中核:判断子节点的差异) 新结束节点和旧结束节点(情况2) 旧结束节点/新开始节点(情况4) 前言 面试官:"你了解虚拟DOM(Virtual DOM)跟Diff算法吗,请描述一下它们"; 我:"额,...鹅

  • vue3 diff 算法示例

    目录 一.可能性(常见): 二.找规律 三.算法优化 最长的递增子序列 完整代码 一.可能性(常见): 1. 旧的:a b c新的:a b c d 2. 旧的:  a b c新的:d a b c 3. 旧的:a b c d新的:a b c 4. 旧的:d a b c新的:  a b c 5. 旧的:a b c d e i f g新的:a b e c d h f g 对应的真实虚拟节点(为方便理解,文中用字母代替): // vnode对象 const a = { type: 'div', // 标

  • vue2从数据变化到视图变化之nextTick使用详解

    目录 引言 1.vue中nextTick的使用场景 2.vue中nextTick在哪里定义 3.vue中nextTick的实现原理 (1)callbacks的定义 (2)timerFunc的定义 (3)cb未传入的处理 4.vue中nextTick的任务分类 小结 引言 nextTick在vue中是一个很重要的方法,在new Vue实例化的同步过程中,将一些需要异步处理的函数推到异步队列中去,可以等new Vue所有的同步任务执行完后,再执行异步队列中的函数. 1.vue中nextTick的使用

  • vue2从数据变化到视图变化之diff算法图文详解

    目录 引言 1.isUndef(oldStartVnode) 2.isUndef(oldEndVnode) 3.sameVnode(oldStartVnode, newStartVnode) 4.sameVnode(oldEndVnode, newEndVnode) 5.sameVnode(oldStartVnode, newEndVnode) 6.sameVnode(oldEndVnode, newStartVnode) 7.如果以上都不满足 小结 引言 vue数据的渲染会引入视图的重新渲染.

  • vue2从数据变化到视图变化发布订阅模式详解

    目录 引言 一.发布订阅者模式的特点 二.vue中的发布订阅者模式 1.dep 2.Object.defineProperty 3.watcher 4.dep.depend 5.dep.notify 6.订阅者取消订阅 小结 引言 发布订阅者模式是最常见的模式之一,它是一种一对多的对应关系,当一个对象发生变化时会通知依赖他的对象,接受到通知的对象会根据情况执行自己的行为. 假设有财经报纸送报员financialDep,有报纸阅读爱好者a,b,c,那么a,b,c想订报纸就告诉financialDe

  • JavaScript设计模式之观察者模式与发布订阅模式详解

    本文实例讲述了JavaScript设计模式之观察者模式与发布订阅模式.分享给大家供大家参考,具体如下: 学习了一段时间设计模式,当学到观察者模式和发布订阅模式的时候遇到了很大的问题,这两个模式有点类似,有点傻傻分不清楚,博客起因如此,开始对观察者和发布订阅开始了Google之旅.对整个学习过程做一个简单的记录. 观察者模式 当对象间存在一对多关系时,则使用观察者模式(Observer Pattern).比如,当一个对象被修改时,则会自动通知它的依赖对象.观察者模式属于行为型模式.在观察模式中共存

  • JS前端设计模式之发布订阅模式详解

    目录 引言 例子1: version1: version2: 总结 引言 昨天我发布了一篇关于策略模式和代理模式的文章,收到的反响还不错,于是今天我们继续来学习前端中常用的设计模式之一:发布-订阅模式. 说到发布订阅模式大家应该都不陌生,它在我们的日常学习和工作中出现的频率简直不要太高,常见的有EventBus.框架里的组件间通信.鉴权业务等等......话不多说,让我们一起进入今天的学习把!!! 发布-订阅模式又叫观察者模式,它定义对象间的一种一对多的依赖关系 当一个对象的状态发生改变时,所有

  • JavaScript实现与使用发布/订阅模式详解

    本文实例讲述了JavaScript实现与使用发布/订阅模式.分享给大家供大家参考,具体如下: 一.发布/订阅模式简介 发布/订阅模式(即观察者模式): 设计该模式背后的主要动力是促进形成松散耦合.在这种模式中,并不是一个对象调用另一个对象的方法,而是一个订阅者对象订阅发布者对象的特定活动,并在发布者对象的状态发生改变后,订阅者对象获得通知.订阅者也称为观察者,而被观察的对象称为发布者或主题.当发生了一个重要的事件时,发布者将会通知(调用)所有订阅者,并且可能经常以事件对象的形式传递消息. 基本思

  • vue2从数据到视图渲染之模板渲染详解

    目录 引言 1.从new Vue()入口开始: 2.this._init 3.挂载函数vm.$mount(vm.$options.el) 4.mountComponent函数 5._render函数 6.createElement函数返回虚拟DOM 7._update函数 8.patch函数 9.createElm函数 10.移除 引言 一般使用html,css和javascript直接将数据渲染到视图中,需要关心如何去操作dom,如果数据量比较大,那么操作过程将会繁琐且不好控制.vue将这些操

  • vue中的watch监听数据变化及watch中各属性的详解

    首先确认 watch是一个对象,一定要当成对象来用. 对象就有键,有值. 键:就是你要监控的那个家伙,比如说$route,这个就是要监控路由的变化.或者是data中的某个变量. 值可以是函数:就是当你监控的家伙变化时,需要执行的函数,这个函数有两个形参,第一个是当前值,第二个是变化后的值. 值也可以是函数名:不过这个函数名要用单引号来包裹. 第三种情况厉害了. 值是包括选项的对象:选项包括有三个. 1.第一个handler:其值是一个回调函数.即监听到变化时应该执行的函数. 2.第二个是deep

  • 发布订阅模式在vue中的实际运用实例详解

    订阅发布模式定义了一种一对多的依赖关系,让多个订阅者对象同时监听某一个主题对象.这个主题对象在自身状态变化时,会通知所有订阅者对象,使它们能够自动更新自己的状态. 比如addEventListener 这个api就是个发布订阅模式 如果用过vue的同学,可以把他类比于 watch 下面我们看一个例子 var observe={ fnsObj:{}, // 订阅方法 on:function(key,fn){ if(!observe.fnsObj[key]){ observe.fnsObj[key]

  • 基于数据类型转换(装箱与拆箱)与常量详解

    隐式转换[自动类型转换]: 两种类型要兼容,原类型值域要小于目标类型值域,可以简单的理解为由小转大. public class Test { private void Start() { int a = 10; float b = a;//int 类型隐式转换为 float 类型 } } 显示转换[强制类型转换]: 两种类型要兼容,原类型值域要大于目标类型值域,可以简单的理解为由大转小. [缺点]:1.数据溢出.2.精度丢失. 数值类型之间的转换. public class Test { pri

随机推荐