Open Closed Principle
Summary
TLDRThis tutorial segment delves into the Open Closed Principle (OCP) of SOLID design principles, emphasizing that software entities should be open for extension but closed for modification. It discusses the principle's importance, implementation guidelines, and the disadvantages of not adhering to it. The session includes a practical example of implementing OCP in a console application, demonstrating the transition from a simple Employee class to an abstract class, allowing for different types of employees to have specific bonus calculations without modifying the base class, thus adhering to the OCP.
Takeaways
- 📘 The Open-Closed Principle (OCP) is one of the SOLID design principles, emphasizing that software entities should be open for extension but closed for modification.
- 👨🏫 Bertrand Meyer is credited with originating the term 'Open-Closed Principle', and it is considered a fundamental principle of object-oriented design by Bob Martin.
- 🔄 To implement OCP, new functionality should be added through new classes, attributes, and methods rather than modifying existing ones.
- 📚 Implementation guidelines suggest deriving new subclasses from the original class or using an abstract interface to access the original class.
- 🚫 Not following OCP can lead to disadvantages such as the need for extensive testing and communication for every change, potentially increasing costs.
- 🔄 Violation of OCP can also break the Single Responsibility Principle, as a class or function may end up handling multiple tasks.
- 💻 The speaker demonstrates the OCP through a coding example, initially creating an Employee class and later modifying it to differentiate bonuses for permanent and contract employees.
- 🛠️ The example shows the process of refactoring the Employee class to an abstract class to adhere to OCP, moving the bonus calculation to derived classes.
- 🔧 Refactoring to an abstract class allows for extending the class for further enhancements without modifying the base class, thus maintaining OCP.
- 🔄 The refactored code creates separate classes for PermanentEmployee and TemporaryEmployee, implementing the bonus calculation specific to each type.
- 🔑 The key takeaway is that adhering to the Open-Closed Principle results in a codebase that is more maintainable and adaptable to change without the need for extensive modifications.
Q & A
What is the Open-Closed Principle (OCP)?
-The Open-Closed Principle states that software entities such as classes, modules, and functions should be open for extension but closed for modification. This means new functionality should be added without changing existing code.
Who is credited with originating the term Open-Closed Principle?
-Bertrand Meyer is generally credited with having originated the term Open-Closed Principle.
Why is the Open-Closed Principle considered important by Bob Martin?
-Bob Martin considers the Open-Closed Principle as the most important principle of object-oriented design because it helps in maintaining and extending software without modifying existing code.
What are the implementation guidelines of the Open-Closed Principle?
-The simplest way to apply the Open-Closed Principle is to implement new functionality in new derived subclasses that inherit the original class. Another way is to allow clients to access the original class with an abstract interface.
What are the disadvantages of not following the Open-Closed Principle during requirement enhancement?
-Not following the Open-Closed Principle can lead to the need for extensive testing of the entire functionality, communication challenges with the quality assurance team for regression testing, and difficulty in maintaining the class due to unorganized code growth.
How does not following the Open-Closed Principle break the Single Responsibility Principle?
-Not adhering to the Open-Closed Principle can result in a class or function performing multiple tasks, thus breaking the Single Responsibility Principle which states that a class or function should have only one reason to change.
What is the example given in the script to demonstrate the Open-Closed Principle?
-The script uses an example of an 'Employee' class where initially a method to compute the bonus is implemented. Later, the requirement changes to differentiate bonuses for permanent and contract employees, demonstrating the need to modify the class, which goes against the Open-Closed Principle.
How is the Open-Closed Principle applied in the 'Employee' class example?
-In the 'Employee' class example, the class is made abstract, and the 'CalculateBonus' method is made abstract as well. Derived classes for permanent and contract employees implement the bonus calculation specific to their type, adhering to the Open-Closed Principle.
What changes are made to the 'Employee' class to adhere to the Open-Closed Principle?
-The 'Employee' class is changed to an abstract class, and the 'CalculateBonus' method is made abstract. The employee type property is removed, and the bonus calculation logic is moved to derived classes for permanent and contract employees.
What is the next principle to be discussed in the tutorial series?
-The next principle to be discussed in the tutorial series is the Liskov Substitution Principle.
Outlines
This section is available to paid users only. Please upgrade to access this part.
Upgrade NowMindmap
This section is available to paid users only. Please upgrade to access this part.
Upgrade NowKeywords
This section is available to paid users only. Please upgrade to access this part.
Upgrade NowHighlights
This section is available to paid users only. Please upgrade to access this part.
Upgrade NowTranscripts
This section is available to paid users only. Please upgrade to access this part.
Upgrade NowBrowse More Related Video
Understanding SOLID Principles in JavaScript
Low Level Design 105 | Single Responsibility Principle in SOLID | 2022 | System Design
SOLID Design Principles Introduction
Single Responsibility Principle
Abstract Classes and Methods in Java Explained in 7 Minutes
23. OCR A Level (H046-H446) SLR5 - 1.2 Open vs closed
5.0 / 5 (0 votes)