Documentos de Académico
Documentos de Profesional
Documentos de Cultura
S.O.L.I.D. STANDSFOR:
S Single responsibility principle
For example, say we have some shapes and we wanted to sum all the
areas of the shapes. Well this is pretty simple right?
First, we create our shapes factory functions and setup the required
parameters.
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 2/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
${this.sum()}
</h1>
}
}
return Object.assign(Object.create(proto), {shapes: s})
}
const shapes = [
circle(2),
square(5),
square(6)
]
const shapes = [
circle(2),
square(5),
square(6)
]
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 3/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
console.log(output.JSON())
console.log(output.HAML())
console.log(output.HTML())
console.log(output.JADE())
Now, whatever logic you need to output the data to the users is now
handled by the sumCalculatorOutputter factory function.
# Open-closed Principle
Objects or entities should be open for extension, but
closed for modi cation.
Open for extension means that we should be able to add new features or
components to the application without breaking existing code.
sum () {
const area = []
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 4/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
A way we can make this sum method better is to remove the logic to
calculate the area of each shape out of the sum method and attach it
to the shapes factory functions.
The same thing should be done for the circle factory function, an
area method should be added. Now, to calculate the sum of any shape
provided should be as simple as:
sum() {
const area = []
for (shape of this.shapes) {
area.push(shape.area())
}
return area.reduce((v, c) => c += v, 0)
}
interface ShapeInterface {
area(): number
}
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 5/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 6/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
And the result of calling the square factory function will be the next
one:
const s = square(5)
console.log('OBJ\n', s)
console.log('PROTO\n', Object.getPrototypeOf(s))
s.area()
// output
OBJ
{ length: 5 }
PROTO
{ type: 'shapeInterface', area: [Function: area] }
25
sum() {
const area = []
for (shape of this.shapes) {
if (Object.getPrototypeOf(shape).type ===
'shapeInterface') {
area.push(shape.area())
} else {
throw new Error('this is not a shapeInterface
object')
}
}
and again, since JavaScript doesnt have support for interfaces like
typed languages the example above demonstrates how we can
simulate it, but more than simulating interfaces, what we are doing is
using closures and function composition if you dont know what are
a closure is this article explains it very well and for complementation
see this video.
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 7/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
Continuing with our shapes example, we know that we also have solid
shapes, so since we would also want to calculate the volume of the
shape, we can add another contract to the shapeInterface:
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 8/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
This is a much better approach, but a pitfall to watch out for is when
to calculate the sum for the shape, instead of using the
shapeInterface or a solidShapeInterface.
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 9/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
As you can see until now, what we have been doing for interfaces in
JavaScript are factory functions for function composition.
If you dont know what a higher order function is you can go and
check this video.
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 10/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
And we have made it before, lets review our code with the
manageShapeInterface and how we accomplish the calculate
method.
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 11/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
)
// more code ...
}
For the square what we need to calculate is just getting the area of the
shape, and for a cubo, what we need is summing the area with the
volume and that is everything need to avoid the coupling and get the
abstraction.
. . .
# Conclusion
If you take the SOLID principles to their extremes, you
arrive at something that makes Functional
Programming look quite attractive MarkSeemann
. . .
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 12/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 13/14
7/26/2017 S.O.L.I.D The rst 5 principles of Object Oriented Design with JavaScript
https://medium.com/@cramirez92/s-o-l-i-d-the-rst-5-priciples-of-object-oriented-design-with-javascript-790f6ac9b9fa 14/14