Created
July 1, 2018 18:52
-
-
Save drenther/f05534658c3cb8c3d1f266a536bd1fd6 to your computer and use it in GitHub Desktop.
Behavioural Pattern - Chain of Responsibility
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
class CumulativeSum { | |
constructor(intialValue = 0) { | |
this.sum = intialValue; | |
} | |
add(value) { | |
this.sum += value; | |
return this; | |
} | |
} | |
// usage | |
const sum1 = new CumulativeSum(); | |
console.log(sum1.add(10).add(2).add(50).sum); // 62 | |
const sum2 = new CumulativeSum(10); | |
console.log(sum2.add(10).add(20).add(5).sum); // 45 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment