Last active
March 15, 2023 02:40
-
-
Save crizstian/ba39c6fdbb30a40c738e3c07ea83b5c1 to your computer and use it in GitHub Desktop.
Code examples of SOLID principles for JavaScript
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
/* | |
Code examples from the article: S.O.L.I.D The first 5 priciples of Object Oriented Design with JavaScript | |
https://medium.com/@cramirez92/s-o-l-i-d-the-first-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa#.7uj4n7rsa | |
*/ | |
const shapeInterface = (state) => ({ | |
type: 'shapeInterface', | |
area: () => state.area(state) | |
}) | |
const solidShapeInterface = (state) => ({ | |
type: 'solidShapeInterface', | |
volume: () => state.volume(state) | |
}) | |
const manageShapeInterface = (fn) => ({ | |
type: 'manageShapeInterface', | |
calculate: () => fn() | |
}) | |
const square = (length) => { | |
const proto = { | |
length, | |
type : 'Square', | |
area : (args) => Math.pow(args.length, 2) | |
} | |
const basics = shapeInterface(proto) | |
const abstraccion = manageShapeInterface(() => basics.area()) | |
const composite = Object.assign({}, basics, abstraccion) | |
return Object.assign(Object.create(composite), {length}) | |
} | |
const circle = (radius) => { | |
const proto = { | |
radius, | |
type: 'Circle', | |
area: (args) => Math.PI * Math.pow(args.radius, 2) | |
} | |
const basics = shapeInterface(proto) | |
const abstraccion = manageShapeInterface(() => basics.area()) | |
const composite = Object.assign({}, basics, abstraccion) | |
return Object.assign(Object.create(composite), {radius}) | |
} | |
const cubo = (length) => { | |
const proto = { | |
length, | |
type : 'Cubo', | |
area : (args) => Math.pow(args.length, 2), | |
volume : (args) => Math.pow(args.length, 3) | |
} | |
const basics = shapeInterface(proto) | |
const complex = solidShapeInterface(proto) | |
const abstraccion = manageShapeInterface(() => basics.area() + complex.volume()) | |
const composite = Object.assign({}, basics, abstraccion) | |
return Object.assign(Object.create(composite), {length}) | |
} | |
const areaCalculator = (s) => { | |
const proto = { | |
type: 'areaCalculator', | |
sum() { | |
const area = [] | |
for (shape of this.shapes) { | |
area.push(shape.calculate()) | |
} | |
return area.reduce((v, c) => c += v, 0) | |
} | |
} | |
return Object.assign(Object.create(proto), {shapes: s}) | |
} | |
const volumeCalculator = (s) => { | |
const proto = { | |
type: 'volumeCalculator' | |
} | |
const areaCalProto = Object.getPrototypeOf(areaCalculator()) | |
const inherit = Object.assign({}, areaCalProto, proto) | |
return Object.assign(Object.create(inherit), {shapes: s}) | |
} | |
const sumCalculatorOputter = (a) => { | |
const proto = { | |
JSON() { | |
return JSON.stringify(this.calculator.sum()) | |
}, | |
HAML() { | |
return `HAML format output` | |
}, | |
HTML() { | |
return ` | |
<h1> | |
Sum of the areas of provided shapes: | |
${this.calculator.sum()} | |
</h1>` | |
}, | |
JADE() { | |
return `JADE format output` | |
} | |
} | |
return Object.assign(Object.create(proto), {calculator: a}) | |
} | |
const shapes = [ | |
circle(2), | |
square(5), | |
square(6) | |
] | |
const solids = [ | |
cubo(4) | |
] | |
const areas = areaCalculator(shapes) | |
const volume = volumeCalculator(solids) | |
const output = sumCalculatorOputter(areas) | |
const output2 = sumCalculatorOputter(volume) | |
console.log(output.JSON()) | |
console.log(output.HAML()) | |
console.log(output.HTML()) | |
console.log(output2.HTML()) | |
console.log(output.JADE()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @crizstian This is kind of a silly question, probably and You most likely already touched on this in the article, but how would you handle multiple interfaces similar to the example found here for c#
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces
If you don't mind clarifying this that would be great. Thanks