13.04 - storing instances for later reuse.js

来自「JS设计模式源代码」· JavaScript 代码 · 共 50 行

JS
50
字号
/* DisplayModule interface. */var DisplayModule = new Interface('DisplayModule', ['show', 'hide', 'state']);/* DialogBox class. */var DialogBox = function() { // implements DisplayModule  ...  };DialogBox.prototype = {  show: function(header, body, footer) { // Sets the content and shows the    ...                                  // dialog box.  },  hide: function() { // Hides the dialog box.    ...  },  state: function() { // Returns 'visible' or 'hidden';    ...  }};/* DialogBoxManager singleton. */var DialogBoxManager = (function() {  var created = []; // Stores created instances.    return {    displayDialogBox: function(header, body, footer) {      var inUse = this.numberInUse(); // Find the number currently in use.      if(inUse > created.length) {        created.push(this.createDialogBox()); // Augment it if need be.      }      created[inUse].show(header, body, footer); // Show the dialog box.    },    createDialogBox: function() { // Factory method.      var db = new DialogBox();      return db;    },    numberInUse: function() {      var inUse = 0;      for(var i = 0, len = created.length; i < len; i++) {        if(created[i].state() === 'visible') {          inUse++;        }      }      return inUse;    }  };})();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?