返回顶部

收藏

中介者模式 (HB Stone)

更多
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="description" content="">
  <script>
    Mediator = function () {
      var debug = function (m) {
            console.info(m)
          },

          components = {},

          addComponent = function (name, component, replaceDuplicate) {
            if (name in components) {
              if (replaceDuplicate) {
                removeComponent(name);
              }
              else {
                throw new Error('Mediator name conflict: ' + name);
              }
            }
            components[name] = component;
          },

          broadcast = function (event, args, source) {
            var c, s,
                e = event || false,
                a = args || [];

            if (!e) { return; }

            for (c in components) {
              if (typeof components[c]["on" + e] == "function") {
                try {
                  s = source || components[c];
                  components[c]["on" + e].apply(s, a);
                }
                catch (err) {
                  debug(["Mediator error.", e, a, s, err].join(' '));
                }ac
              }
            }
          },
          removeComponent = function (name) {
            if (name in components) { delete components[name]; }
          },
          getComponent = function (name) { return components[name] || false; },
          contains = function (name) { return (name in components); };

      return {
        name: "Mediator",
        broadcast: broadcast,
        add: addComponent,
        rem: removeComponent,
        get: getComponent,
        has: contains
      };
    }();

    Mediator.add('TestObject', function () {
      var someNumber = 0,    // sample variable
          someString = 'another sample variable';

      return {
        onInitialize: function () {
                  console.debug(this)
          // this.name is automatically assigned by the Mediator
          console.log(this.name + " initialized.");
        },
        onFakeEvent: function () {
          someNumber++;
          console.log("Handled " + someNumber + " times!");
        },
        onSetString: function (str) {
          someString = str;
          console.log('Assigned ' + someString);
        }
      }
    }());

  </script>

</head>
<body>

  <script>
    Mediator.broadcast("Initialize"); // alerts "TestObject initialized"
    Mediator.broadcast('FakeEvent'); // alerts "Handled 1 times!" (I know, bad grammar)
    Mediator.broadcast('SetString', ['test string']); // alerts "Assigned test string"
    Mediator.broadcast('FakeEvent'); // alerts "Handled 2 times!"
    Mediator.broadcast('SessionStart'); // this call is safely ignored
    Mediator.broadcast('Translate', ['this is also safely ignored']);
  </script>

</body>
</html>

标签:javascript

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. K-Res 发表 2018-10-27 12:10:11 关于Javascript中类成员函数中内嵌函数的this闭包问题
  2. 杨魁 发表 2018-10-25 07:28:13 Under the Hood: NaN of JS
  3. 尖兵 发表 2018-10-25 11:03:47 服务重启导致的Java服务抖动CPU占用高
  4. 可乐加糖 发表 2018-10-23 08:31:16 JavaScript文档生成器JSDuck
  5. 可乐加糖 发表 2018-10-23 08:31:16 JavaScript文档生成器JSDuck
  6. wenming.gapo 发表 2018-10-23 11:32:30 AOP装饰函数与小T的情愫(二)
  7. 尖兵 发表 2018-10-18 13:19:02 Java诊断工具Arthas
  8. 尖兵 发表 2018-10-18 13:19:02 Java诊断工具Arthas
  9. hellas 发表 2018-10-18 14:20:31 AOP装饰函数与小T的情愫
  10. 博主 发表 2018-10-15 10:02:38 生成聚合收款二维码:支付宝、微信、QQ 钱包
  11. axiu 发表 2018-10-14 02:30:40 微信小程序开发总结
  12. xirruiqiang 发表 2018-10-14 08:14:43 round 函数在不同语言中的实现乱象

发表评论