Skip to content

Instantly share code, notes, and snippets.

@drenther
Created July 1, 2018 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drenther/f05534658c3cb8c3d1f266a536bd1fd6 to your computer and use it in GitHub Desktop.
Save drenther/f05534658c3cb8c3d1f266a536bd1fd6 to your computer and use it in GitHub Desktop.
Behavioural Pattern - Chain of Responsibility
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