1、中介者模式的作用就是解除对象与对象之间的紧耦合关系。增加一个中介者对象后,所有的相关对象都通过中介者来通信,而不是互相引用,所以当一个对象发生改变时,只需要通知中介对象即可。中介者使各对象之间耦合松散,而且可以独立的改变他们之间的交互。中介者模式使网状的多对多关系变成了相对简单的一对多关系。

<select id="colorselect">
<option value="">请选择颜色</option>
<option value="red">红色</option>
<option value="blue">蓝色</option>
</select> <select name="" id="memoryselect">
<option value="">请选择大小</option>
<option value="16G">16G</option>
<option value="32G">32G</option>
</select> 请输入购买数量:
<input type="text" id="numberInput" /><br/> 您选择了颜色:
<div id="colorInfo"></div><br />
您选择了内存:
<div id="memoryInfo"></div><br />
您输入了数量:
<div id="numberInfo"></div><br /> <button id="nextBtn" disabled="true">请选择手机颜色和购买数量</button>
<script type="text/javascript">

	var colorselect = document.getElementById("colorselect");
var memoryselect = document.getElementById("memoryselect");
var numberInput = document.getElementById("numberInput");
var colorInfo = document.getElementById("colorInfo");
var memoryInfo = document.getElementById("memoryInfo");
var numberInfo = document.getElementById("numberInfo"); var goods = { "red|32G":3,
"red|16G":0,
"blue|32G":1,
"blue|16G":6
} var mediator = (function(obj){ return {
change:function(obj){ var color = colorselect.value,
memory = memoryselect.value; if(obj == colorselect){
colorInfo.innerHTML = color;
}else if(obj == memoryselect){
memoryInfo.innerHTML = memory;
} }
}
})(); colorselect.onchange = function(){
mediator.change(this);
}
memoryselect.onchange = function(){
mediator.change(this);
}
numberInput.oninput = function(){
mediator.change(this);
}

学习javascript设计模式之中介者模式的更多相关文章

  1. 再起航,我的学习笔记之JavaScript设计模式23(中介者模式)

    中介者模式 概念介绍 中介者模式(Mediator):通过中介者对象封装一系列对象之间的交互,使对象之间不再相互引用降低他们之间的耦合,有时中介者对象也可以改变对象之间的交互. 创建一个中介 中介者模 ...

  2. javascript设计模式之中介者模式

    /* * 小游戏演示中介者模式 * Home 按键 1 * Guest 按键 0 * 半分钟内看谁按下的次数多 * * 参与的对象: * 玩家 * 计分板 * 中介者 * * 中介者模式使对象之间松耦 ...

  3. 学习javascript设计模式之装饰者模式

    1.装饰者模式定义:给对象动态添加职责的方式称为装饰者(decorator)模式. js如何实现装饰者模式 通过保存原函数引用方式改写某函数 window.onload = function(){al ...

  4. javaScript 设计模式之中介者模式示例

    飞机把注册信息放到铁塔里,发送数据到铁塔,报告其它的飞机一些信息. var feiji = function( name ){ this.name = name; } feiji.prototype. ...

  5. 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)

    原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...

  6. 折腾Java设计模式之中介者模式

    博文原址:折腾Java设计模式之中介者模式 中介者模式 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性.这种模式提供了一个中介类,该类通常处理不同类之间的通信,并 ...

  7. [转] JavaScript设计模式之发布-订阅模式(观察者模式)-Part1

    <JavaScript设计模式与开发实践>读书笔记. 发布-订阅模式又叫观察者模式,它定义了对象之间的一种一对多的依赖关系.当一个对象的状态发生改变时,所有依赖它的对象都将得到通知. 例如 ...

  8. js设计模式——8.中介者模式

    js设计模式——8.中介者模式 /*js设计模式——中介者模式*/ class A { constructor() { this.number = 0; } setNumber(num, m) { t ...

  9. javascript设计模式--策略模式

    javascript策略模式总结 1.什么是策略模式? 策略模式的定义是:定义一系列的算法,把他们独立封装起来,并且可以相互替换. 例如我们需要写一段代码来计算员工的奖金.当绩效为a时,奖金为工资的5 ...

随机推荐

  1. virtualvenv+django+uWSGI+nginx 部署

    原创博文 转载请注明出处! 1. virtualvenv 2. django 3. uWSGI 4. nginx 5. 踩坑记录 1. virtualvenv virtualvenv install ...

  2. python的标准模块

    本文用于记录python中的标准模块,随时更新. decimal模块(解决小数循环问题): >>> import decimal >>> a = decimal.D ...

  3. linux防火墙firewall使用简介

    1.firewalld的基本使用启动: systemctl start firewalld查看状态: systemctl status firewalld停止: systemctl disable f ...

  4. Nordic Collegiate Programming Contest 2015​ E. Entertainment Box

    Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...

  5. $ cat /usr/share/doc/wireshark-common/README.Debian

    $ cat /usr/share/doc/wireshark-common/README.Debian I. Capturing packets with Wireshark/Tshark There ...

  6. HDU 4990 Reading comprehension 矩阵快速幂

    题意: 给出一个序列, \(f_n=\left\{\begin{matrix} 2f_{n-1}+1, n \, mod \, 2=1\\ 2f_{n-1}, n \, mod \, 2=0 \end ...

  7. BNU 3692 I18n 模拟

    题意: 在一篇文章中,单词可以缩写.例如单词Internationalization可以缩写为I18n,缩写的两端是原单词的首尾字母,中间的数字是被省略的字母的个数. 现在给你一篇缩写的文章,输出展开 ...

  8. managed unmanaged

    Enable function-level control for compiling functions as managed or unmanaged.     #pragma managed # ...

  9. MyInt的简单实现

    #include <iostream> using namespace std; class CMyInt{ private: int value; public: CMyInt(int ...

  10. luogu4001 [BJOI2006]狼抓兔子

    裸dinic就跑过去了,哪用得着平面图最小割=最短路-- #include <iostream> #include <cstring> #include <cstdio& ...