Skip to content

Instantly share code, notes, and snippets.

View kudapara's full-sized avatar
🌀
Verified

Kudakwashe Paradzayi kudapara

🌀
Verified
View GitHub Profile
@kudapara
kudapara / violates-ocp.js
Created April 18, 2018 19:22
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)
@kudapara
kudapara / shapes-in-use.js
Last active April 18, 2018 20:00
Using the shapes file
const circle = new Circle(7)
const square = new Square(3)
const shapes = [circle, square]
const areaCalculator = new AreaCalculator(shapes)
const areaOutput = new CalculatorOutput(areaCalculator)
// now we can access each output type
console.log(output.toString())
console.log(output.toJSON())
@kudapara
kudapara / shapes2.js
Last active April 18, 2018 19:19
Code that does not violate SRP
class AreaCalculator {
constructor (shapes) {
this.shapes = shapes
}
sum () {
// logic to calculate the sum of the areas of the provided shapes
}
}
@kudapara
kudapara / shapes1.js
Last active April 18, 2018 19:57
Code violating SRP
class Circle {
constructor (radius) {
this.radius = radius
}
}
class Square {
constructor (length) {
this.length = length
}