The second SOLID principle concerns the extensibility of components and is called the Open/Closed Principle. Its focus is on avoiding changes when we need to extend a component's feature. The principle states:
Software entities like classes, modules and functions should be open for extension but closed for modifications.
In the design of the components of our application, we have to take into account these two aspects:
Open for extension: The components should be adjustable to the changing needs of the application
Closed for modifications: The required changes should not involve the original component itself
If we apply this principle, we can get more easily adaptable and maintainable applications.
To illustrate how to apply this principle, let's take the example of order management in the previous section with some small modifications:
function Order(customerId) {
this.customerId = customerId;
this.dateTime = new Date();
this.totalAmount = 0;
this.items...