Skip to content

Instantly share code, notes, and snippets.

@kudapara
Created April 18, 2018 19:22
Show Gist options
  • Save kudapara/efc3e27411fae9bc08945a34a5a5a780 to your computer and use it in GitHub Desktop.
Save kudapara/efc3e27411fae9bc08945a34a5a5a780 to your computer and use it in GitHub Desktop.
Code that voilates OCP
class AreaCalculator {
constructor (shapes) {
this.shapes = shapes
}
sum () {
return this.shapes
.map((shape) => {
if (shape instanceof Circle) {
return 2 * Math.PI * Math.pow(shape.radius, 2)
}
else if (shape instanceof Square) {
return Math.pow(shape.length, 2)
}
})
.reduce((totalArea, currentArea) => {
return totalArea + currentArea
}, 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment