-
-
Save gabouh/2de36aa157b43d4d1399e329127678a9 to your computer and use it in GitHub Desktop.
Sample Application using Module, Facade, and Mediator Patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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; | |
} | |
}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function(window, mediator) { | |
var orderFacadeMediator = window.orderFacadeMediator = {}; | |
orderFacadeMediator.subscribe = mediator.subscribe | |
orderFacadeMediator.publish = mediator.publish | |
orderFacadeMediator.installTo = mediator.installTo; | |
}(window, CoreMediator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!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