October 2018
Intermediate to advanced
590 pages
15h 5m
English
A software artifact should be open for extension but closed for modification.
Bertrand Meyer coined the OCP in 1988, providing us with guidance on how to create a design that is stable in the face of changes.
Here, we're going to borrow the DrawAllShapes example from Agile Software Development: Principles, Patterns, and Practices, to explain this principle here. We will write the example in ES6; the unrelated details have been omitted:
class Circle extends Shape { constructor(radius, point) { this.type = 'circle' // ... }}class Square extends Shape { constructor(width, point) { this.type = 'square' // ... }}function drawCircle(circle) { // Draw circle logic here}function drawSquare(square) { // Draw square ...Read now
Unlock full access