Skip to content

Instantly share code, notes, and snippets.

@madzak
Last active January 6, 2019 23:22
Show Gist options
  • Save madzak/5453259 to your computer and use it in GitHub Desktop.
Save madzak/5453259 to your computer and use it in GitHub Desktop.
Sample Application using Module, Facade, and Mediator Patterns
!function() {
var CoreMediator = window.CoreMediator = {},
channels = [];
CoreMediator.subscribe = function(channel, func) {
if (!channels[channel]) {
channels[channel] = [];
}
channels[channel].push({ context: this, callback: func });
};
CoreMediator.publish = function(channel) {
if(!channels[channel]) {
return false;
}
var args = Array.prototype.slice.call(arguments, 1);
for (var i=0,l=channels[channel].length; i<l; i=i+1) {
var subscription = channels[channel][i];
subscription.callback.apply(subscription.context, args);
}
};
CoreMediator.installTo = function(obj) {
obj.subscribe = this.subscribe;
obj.publish = this.publish;
}
}();
!function(window) {
var dispatchManager = window.dispatchManager = {};
dispatchManager.dispatchOrder = function(ordernum) {
console.log("Dispatching order for #"+ordernum);
setTimeout("dispatchManager.dispatchComplete("+ordernum+")", 2000);
}
dispatchManager.dispatchComplete = function(ordernum) {
console.log("Dispatched order for #"+ordernum);
dispatchManager.publish("onDispatch", ordernum);
}
orderFacadeMediator.installTo(dispatchManager);
dispatchManager.subscribe("onDispatchOrder", function(arg) {
console.log("dispatchManager Start");
dispatchManager.dispatchOrder(arg);
});
}(window);
<html>
<head>
<title>Order Sample</title>
<script type="text/javascript" src="core_mediator.js"></script>
<script type="text/javascript" src="order_facade_mediator.js"></script>
<script type="text/javascript" src="order_workflow_director.js"></script>
<script type="text/javascript" src="order_packager_module.js"></script>
<script type="text/javascript" src="label_manager_module.js"></script>
<script type="text/javascript" src="dispatch_manager_module.js"></script>
<script type="text/javascript" src="order_status_display_module.js"></script>
</head>
<body>
<h1>Order System</h1>
<button onclick="orderFacadeMediator.publish('onOrder', '432534325234');">Place Order</button>
</body>
</html>
!function(window) {
var labelManager = window.labelManager = {};
labelManager.makeLabel = function(ordernum) {
console.log("Creating label for #"+ordernum);
setTimeout("labelManager.labelComplete("+ordernum+")", 3000);
}
labelManager.labelComplete = function(ordernum) {
console.log("Label completed for #"+ordernum);
labelManager.publish("onLabel", ordernum);
}
orderFacadeMediator.installTo(labelManager);
labelManager.subscribe("onCreateLabel", function(arg) {
console.log("labelManager Start Label");
labelManager.makeLabel(arg);
});
}(window);
!function(window, mediator) {
var orderFacadeMediator = window.orderFacadeMediator = {};
orderFacadeMediator.subscribe = mediator.subscribe
orderFacadeMediator.publish = mediator.publish
orderFacadeMediator.installTo = mediator.installTo;
}(window, CoreMediator);
!function(window) {
var orderPackager = window.orderPackager = {};
orderPackager.packageOrder = function(ordernum) {
console.log("Packaging order #"+ordernum);
setTimeout("orderPackager.packageComplete("+ordernum+")", 1000);
};
orderPackager.packageComplete = function(ordernum) {
console.log("Package completed for order #"+ordernum);
orderPackager.publish("onPackage", ordernum);
};
orderFacadeMediator.installTo(orderPackager);
orderPackager.subscribe("onOrderPlaced", function(arg) {
console.log("Order Packaging Starting for #"+arg);
orderPackager.packageOrder(arg);
});
}(window);
!function(window, document) {
var orderStatusDisplay = window.orderStatusDisplay = {};
orderStatusDisplay.updateDisplay = function(order_status) {
document.write("<h2>Status: "+order_status+"</h2>");
}
orderFacadeMediator.installTo(orderStatusDisplay);
orderStatusDisplay.subscribe("onPackage", function(arg) {
document.write("<h1>Order #"+arg+"</h1>");
orderStatusDisplay.updateDisplay("Order Packaged");
});
orderStatusDisplay.subscribe("onLabel", function(arg) {
orderStatusDisplay.updateDisplay("Label Created");
});
orderStatusDisplay.subscribe("onDispatch", function(arg) {
orderStatusDisplay.updateDisplay("Order Dispatched");
});
}(window, document);
!function(window) {
var orderWorkflowDirector = window.orderWorkflowDirector = {};
orderFacadeMediator.installTo(orderWorkflowDirector);
orderWorkflowDirector.subscribe("onOrder", function(arg) {
console.log("Mediator order placed #"+arg);
orderWorkflowDirector.publish("onOrderPlaced", arg);
});
orderWorkflowDirector.subscribe("onPackage", function(arg) {
console.log("Mediator package completed #"+arg);
orderWorkflowDirector.publish("onCreateLabel", arg);
});
orderWorkflowDirector.subscribe("onLabel", function(arg) {
console.log("Mediator label completed #"+arg);
orderWorkflowDirector.publish("onDispatchOrder", arg);
});
orderWorkflowDirector.subscribe("onDispatch", function(arg) {
console.log("Mediator dispatch order completed #"+arg);
});
}(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment