Open Closed Principle: Add Features Without Breaking Existing Code
The Open Closed Principle (OCP) is a software design guideline stating that code should be open for extension but closed for modification, meaning new functionality can be added without altering existing source code. This approach improves stability by reducing the risk of introducing bugs into already-working features when changes are made. A practical example involves shipping cost calculators, where adding a new carrier like DHL traditionally required editing a shared file and risking breakage of existing logic for FedEx and UPS. Under OCP, a standard interface is defined so each carrier is implemented as its own isolated class, allowing the core calculator to remain unchanged regardless of how many carriers are added. Developers should note that bug fixes still require modifying existing files, and interfaces should not be created unnecessarily when only one implementation will ever exist.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in