Skip to content

Instantly share code, notes, and snippets.

@hwclass
Last active March 1, 2016 23:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hwclass/335610035971df6ba1c8 to your computer and use it in GitHub Desktop.
Save hwclass/335610035971df6ba1c8 to your computer and use it in GitHub Desktop.
Design Patterns : Module Pattern
/**
* @fileoverview The setter for the modules into the main context
* @author hwclass
*/
'use strict';
//-----------------------------------------------------------
// Public
//-----------------------------------------------------------
/**
* register method
* @param {Object} Module
* @param {Function}
*/
register.module(new Module('testModule', function () {
console.log('testModule initialized.');
}));
/**
* Initialization invoked
* @returns {void}
*/
init.module(‘testModule’);
/**
* @fileoverview The global file to attach modules into an array called MODULES
* @author hwclass
*/
/**
* global.js
* This part of code is the main layer that the other
* modules are attached into an array to be fetched
* during an immediate usage in this tiny structure
*/
'use strict';
//-----------------------------------------------------------
// Public
//-----------------------------------------------------------
/** @type {Array} */
var MODULES = MODULES || [];
/**
* @fileoverview The main initialization method to start modules working
* @author hwclass
*/
/**
* init.js
* This method realize the binded modules in our application
* with their context to be invoked
*/
'use strict';
//-----------------------------------------------------------
// Public
//-----------------------------------------------------------
/**
* register method
* @returns {Function} module
*/
var init = (function () {
return {
/**
* module method
* @param {String} moduleName
*/
module : function (moduleName) {
for (var counter = 0, len = MODULES.length; counter < len; counter++) {
if (MODULES[counter]['name'] === moduleName) {
MODULES[counter]['context']();
}
}
}
}
})();
/**
* @fileoverview The base object to generate modules
* @author hwclass
*/
/**
* models/Module.js
* This object is used to generate some instances
* as modules to be attached into the MODULES array.
*/
'use strict';
//-----------------------------------------------------------
// Public
//-----------------------------------------------------------
/**
* Module object
* @param {String} name
* @param {Function} context
*/
var Module = function (name, context) {
this.name = name;
this.context = context;
}
/**
* @fileoverview The module registration file to attach modules into the context
* @author hwclass
*/
/**
* register.js
* This method sets the module with two parameter
* called name as string and context as a pure function
* into MODULES array as a Module reference
*/
'use strict';
//-----------------------------------------------------------
// Public
//-----------------------------------------------------------
/**
* register method
* @noparam
*/
var register = (function () {
return {
module : function (module) {
MODULES.push(new Module(module.name, module.context));
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment