Last active
February 19, 2016 16:31
-
-
Save ericelliott/9ddf8562ca3826af238d to your computer and use it in GitHub Desktop.
Black box composition example
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
const pooper = (delay) => { | |
return { | |
lastPoop: null, | |
getPoopDelay () { return delay; }, | |
canPoop: function(now) { | |
return this.lastPoop ? (this.lastPoop + delay) >= now : true | |
}, | |
poop: function(){ | |
if(this.canPoop(new Date())){ | |
//implementation for pooping is omitted | |
this.lastPoop = new Date(); | |
} | |
} | |
} | |
}; | |
const eater = (delay) => { | |
return { | |
lastMeal: null, | |
getEatDelay () { return delay; }, | |
canEat: function(now) { | |
return this.lastMeal ? (this.lastMeal + delay) >= now : true | |
}, | |
eat: function(food){ | |
if(this.canEat(new Date())){ | |
//implementation for eating is omitted | |
this.lastMeal = new Date(); | |
} | |
} | |
}; | |
}; | |
const dog = (poopDelay, eatDelay, pooper, eater) => Object.assign({}, pooper(poopDelay), eater(eatDelay)); | |
let myDog = dog(4, 8, pooper, eater); | |
console.log(myDog.getPoopDelay()) //4 | |
console.log(myDog.getEatDelay()) //8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, does
this.delay
need tidying up?