var Waiter = function(name) {
this.name = name;
this.orders = ['chicken', 'beef', 'vegan'];
};
Waiter.prototype.placeOrder = function(name) {
if (this.orders.indexOf(name) !== -1) {
console.log(this.name, name + ' is up!');
this.sousChef.receiveOrder(name);
} else {
console.log(this.name, 'I am a waiter. I cannot handle unknown dishes');
}
};
Waiter.prototype.assignSousChef = function(sousChef) {
this.sousChef = sousChef;
};
// Sous Chef acts as the observer
var SousChef = function(name) {
this.name = name;
this.commands = {};
this.orders = {
'chicken': ['grill', 'steam'],
'beef': ['grill', 'steam'],
'vegan': ['steam', 'boil']
};
};
// Sous Chef tell the kitchen what to do, so we give the staff a way to listen to her
SousChef.prototype.dispatchesOrder = function(toCommand, action) {
if (!Array.isArray(this.commands[toCommand])) {
this.commands[toCommand] = [];
}
this.commands[toCommand].push(action);
};
// Sous Chef can give orders out
SousChef.prototype.handleOrder = function(toCommand) {
if (Array.isArray(this.commands[toCommand])) {
this.commands[toCommand].forEach(function(instance) {
instance();
});
}
};
// Sous chef can also be told what to do
SousChef.prototype.receiveOrder = function(name) {
console.log(this.name, 'Got it! telling the kitchen to make ' + name);
this.orders[name].forEach(function(instruction) {
this.handleOrder(instruction);
}.bind(this));
};
// the different cooks listen to the SousChef about different things
var Griller = function(name) {
this.name = name;
this.chef = terry;
this.isBusy = false;
terry.dispatchesOrder('grill', function() {
console.log(this.name, 'I am grilling!');
this.isBusy = true;
setTimeout(function() {
console.log(this.name, 'Done Grilling');
this.isBusy = false;
}.bind(this), 5000);
}.bind(this));
};
var NoodleCook = function(name) {
this.name = name;
this.chef = terry;
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.