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 / 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
}
@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 / 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 / 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 / adheres-to-ocp.js
Created April 18, 2018 19:42
Code that adheres to OCP
class Circle {
constructor (radius) {
this.radius = radius
}
area () {
return 2 * Math.PI * Math.pow(shape.radius, 2)
}
}
@kudapara
kudapara / adheres-to-ocp.js
Created April 18, 2018 19:44
Code that adheres to OCP
class Circle {
constructor (radius) {
this.radius = radius
}
area () {
return 2 * Math.PI * Math.pow(shape.radius, 2)
}
}
@kudapara
kudapara / violates-isp.js
Created April 18, 2018 19:49
Code that violates ISP
class Shape {
constructor () {}
area () {}
volume () {}
}
class Triangle extends Shape {
constructor (base, height) {
this.base = base
this.height = height
@kudapara
kudapara / adheres-to-isp.js
Created April 18, 2018 19:50
Code that adheres to ISP
class Shape {
constructor () {}
area () {}
}
class SolidShape {
constructor () {}
volume () {}
}
@kudapara
kudapara / violates-dip.js
Created April 18, 2018 19:53
Code that violates DIP
class Shape {
constructor () {}
setColor(color) {
this.color = color
}
}
class Picture {
constructor (color) {
this.shape = new Shape()
@kudapara
kudapara / adheres-to-dip.js
Created April 18, 2018 19:55
Code that adheres to DIP
class Shape {
constructor () {}
setColor (color) {
this.color = color
}
}
class Triangle extends Shape {
constructor (base, height) {