đĨ02A-Open-Closed Principle (OCP)
The Open-Closed Principle (OCP) is one of the five SOLID principles of object-oriented design. It states that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. This means that you should be able to add new functionality to existing code without altering its source code.
Key Concepts
-
Closed for Modification:
- Existing code should not be changed when adding new features. This helps avoid introducing bugs and maintains the stability of the system.
- New features getting added to the software component should not have to modify the existing code.
-
Open for Extension:
- New functionalities can be added through new code, allowing the system to grow without modifying existing components.
Real-World Analogy
A practical analogy to understand OCP is the Nintendo Wii [Joy Stick] tool gaming console:
- The Wii console comes with a basic controller and allows for various accessories (like the Wii Zapper and steering wheel) to be added without modifying the console itself.
- This design ensures that users can enhance their gaming experience without needing to alter the core system.
Benefits of OCP
- Maintainability: Reduces the risk of bugs by keeping existing code untouched.
- Scalability: Facilitates easy addition of new features as requirements evolve.
- Reusability: Encourages the reuse of existing modules and classes in different contexts.
Implementation Example in Java
To illustrate OCP in practice, consider a scenario where you need to compare areas of different shapes:
Without OCP
class Square {
int height;
int area() { return height * height; }
}
class OpenOpenExample {
public int compareArea(Square a, Square b) {
return a.area() - b.area();
}
}
This approach requires modification if a new shape (like Circle) is introduced.
With OCP
By using an interface:
interface Shape {
int area();
}
class Circle implements Shape {
int radius;
int area() { return (int)(Math.PI * radius * radius); }
}
class Square implements Shape {
int height;
int area() { return height * height; }
}
class OpenClosedExample {
public int compareArea(Shape a, Shape b) {
return a.area() - b.area();
}
}
In this design, you can add new shapes without modifying existing code, adhering to the Open-Closed Principle.
Conclusion
The Open-Closed Principle encourages developers to design systems that are robust and adaptable.
By focusing on extending functionality rather than modifying existing code, developers can create more maintainable and scalable applications.
In future sessions, we will explore practical coding examples that demonstrate OCP in action.
đĨ02B-āĻāĻĒā§āĻ¨-āĻā§āĻ˛ā§āĻāĻĄ āĻĒā§āĻ°āĻŋāĻ¨ā§āĻ¸āĻŋāĻĒāĻ˛: āĻā§āĻĄ āĻāĻĻāĻžāĻšāĻ°āĻŖ
āĻĒāĻ°āĻŋāĻāĻŋāĻ¤āĻŋ
āĻāĻŽāĻ°āĻž āĻāĻāĻāĻŋ āĻā§āĻĄ āĻāĻĻāĻžāĻšāĻ°āĻŖ āĻĻā§āĻāĻŦ āĻ¯āĻž āĻāĻ āĻ¨ā§āĻ¤āĻŋāĻ° āĻŦāĻžāĻ¸ā§āĻ¤āĻŦāĻžāĻ¯āĻŧāĻ¨āĻā§ āĻāĻŋāĻ¤ā§āĻ°āĻŋāĻ¤ āĻāĻ°āĻŦā§āĨ¤
āĻĒā§āĻ°ā§āĻā§āĻˇāĻžāĻĒāĻ
āĻ§āĻ°āĻŋ, One State āĻāĻāĻāĻŋ āĻŦā§āĻŽāĻž āĻā§āĻŽā§āĻĒāĻžāĻ¨āĻŋ āĻ¯āĻž āĻŽā§āĻ˛āĻ¤ āĻ¸ā§āĻŦāĻžāĻ¸ā§āĻĨā§āĻ¯ āĻŦā§āĻŽāĻžāĻ° āĻ¸āĻžāĻĨā§ āĻāĻĄāĻŧāĻŋāĻ¤āĨ¤ āĻ¤āĻžāĻĻā§āĻ° āĻŦā§āĻŽāĻž āĻāĻŖāĻ¨āĻž āĻāĻāĻāĻŋ Java āĻ˛āĻžāĻāĻŦā§āĻ°ā§āĻ°āĻŋāĻ¤ā§ āĻā§āĻĄ āĻāĻ°āĻž āĻšāĻ¯āĻŧā§āĻā§āĨ¤
āĻā§āĻĄ āĻ¸ā§āĻ¨āĻŋāĻĒā§āĻ
āĻāĻāĻžāĻ¨ā§ āĻāĻāĻāĻŋ āĻā§āĻĄ āĻ¸ā§āĻ¨āĻŋāĻĒā§āĻ āĻ°āĻ¯āĻŧā§āĻā§ āĻ¯āĻž āĻĒā§āĻ°āĻŋāĻŽāĻŋāĻ¯āĻŧāĻžāĻŽ āĻĄāĻŋāĻ¸āĻāĻžāĻāĻ¨ā§āĻ āĻā§āĻ¯āĻžāĻ˛āĻā§āĻ˛ā§āĻļāĻ¨ āĻĻā§āĻāĻžāĻ¯āĻŧ:
class InsurancePremiumDiscountCalculator {
public double calculatePremiumDiscountPercent(HealthInsuranceCustomerProfile profile) {
if (profile.isLoyalCustomer()) {
return 10.0; // Loyal customer discount
}
return 0.0; // No discount
}
}
-
HealthInsuranceCustomerProfile āĻā§āĻ˛āĻžāĻ¸ā§ āĻāĻāĻāĻŋ
isLoyalCustomer()
āĻĒāĻĻā§āĻ§āĻ¤āĻŋ āĻ°āĻ¯āĻŧā§āĻā§ āĻ¯āĻž āĻ¸āĻ¤ā§āĻ¯ (true) āĻĢā§āĻ°āĻ¤ āĻĻā§āĻ¯āĻŧ āĻ¯āĻĻāĻŋ āĻā§āĻ°āĻžāĻšāĻ āĻāĻāĻāĻ¨ āĻŦāĻŋāĻļā§āĻŦāĻ¸ā§āĻ¤ āĻā§āĻ°āĻžāĻšāĻ āĻšāĻ¯āĻŧ, āĻ āĻ¨ā§āĻ¯āĻĨāĻžāĻ¯āĻŧ āĻŽāĻŋāĻĨā§āĻ¯āĻž (false) āĻĢā§āĻ°āĻ¤ āĻĻā§āĻ¯āĻŧāĨ¤
āĻ¨āĻ¤ā§āĻ¨ āĻā§āĻ¯āĻžāĻ˛ā§āĻā§āĻ
āĻ§āĻ°āĻŋ, One State āĻā§āĻŽā§āĻĒāĻžāĻ¨āĻŋ āĻāĻāĻāĻŋ āĻ¨āĻ¤ā§āĻ¨ āĻāĻžāĻĄāĻŧāĻŋ āĻŦā§āĻŽāĻž āĻā§āĻŽā§āĻĒāĻžāĻ¨āĻŋ āĻ āĻ§āĻŋāĻā§āĻ°āĻšāĻŖ āĻāĻ°ā§ āĻāĻŦāĻ āĻ¤āĻžāĻĻā§āĻ° āĻā§āĻ¯āĻžāĻāĻ˛āĻžāĻāĻ¨ āĻĒāĻ°āĻŋāĻŦāĻ°ā§āĻ¤āĻ¨ āĻāĻ°ā§: "āĻāĻĒāĻ¨āĻžāĻ° āĻ¸ā§āĻŦāĻžāĻ¸ā§āĻĨā§āĻ¯ āĻāĻŦāĻ āĻāĻžāĻĄāĻŧāĻŋ āĻŦā§āĻŽāĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻ¸āĻŦāĻāĻŋāĻā§"āĨ¤ āĻāĻāĻ¨ āĻāĻŽāĻžāĻĻā§āĻ° āĻāĻžāĻĄāĻŧāĻŋ āĻŦā§āĻŽāĻžāĻ° āĻĄāĻŋāĻ¸āĻāĻžāĻāĻ¨ā§āĻāĻ āĻ¸āĻŽāĻ°ā§āĻĨāĻ¨ āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
āĻ¨āĻ¤ā§āĻ¨ āĻā§āĻ˛āĻžāĻ¸ āĻ¯ā§āĻ āĻāĻ°āĻž
āĻāĻŽāĻ°āĻž āĻāĻāĻāĻŋ āĻ¨āĻ¤ā§āĻ¨ āĻā§āĻ˛āĻžāĻ¸ āĻ¯ā§āĻā§āĻ¤ āĻāĻ°āĻŋ: VehicleInsuranceCustomerProfileāĨ¤ āĻāĻāĻŋ HealthInsuranceCustomerProfile āĻāĻ° āĻŽāĻ¤ā§ āĻāĻŦāĻ āĻāĻ° isLoyal()
āĻĒāĻĻā§āĻ§āĻ¤āĻŋ āĻ°āĻ¯āĻŧā§āĻā§āĨ¤
class VehicleInsuranceCustomerProfile {
public boolean isLoyal() {
// Logic to determine loyalty
}
}
āĻ¸āĻŽāĻ¸ā§āĻ¯āĻž
āĻāĻāĻ¨ āĻāĻŽāĻžāĻĻā§āĻ° InsurancePremiumDiscountCalculator āĻā§āĻ˛āĻžāĻ¸āĻāĻŋ āĻĒāĻ°āĻŋāĻŦāĻ°ā§āĻ¤āĻ¨ āĻāĻ°āĻ¤ā§ āĻšāĻŦā§, āĻāĻžāĻ°āĻŖ āĻāĻ° calculate
āĻĒāĻĻā§āĻ§āĻ¤āĻŋ āĻŦāĻ°ā§āĻ¤āĻŽāĻžāĻ¨ā§ āĻļā§āĻ§ā§āĻŽāĻžāĻ¤ā§āĻ° HealthInsuranceCustomerProfile āĻ
āĻŦāĻā§āĻā§āĻ āĻā§āĻ°āĻšāĻŖ āĻāĻ°ā§āĨ¤
OCP āĻ˛āĻā§āĻāĻ¨
- āĻ¨āĻ¤ā§āĻ¨ āĻŦā§āĻļāĻŋāĻˇā§āĻā§āĻ¯ āĻ¯ā§āĻ āĻāĻ°āĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻāĻŽāĻžāĻĻā§āĻ° āĻŦāĻŋāĻĻā§āĻ¯āĻŽāĻžāĻ¨ āĻā§āĻĄā§ āĻĒāĻ°āĻŋāĻŦāĻ°ā§āĻ¤āĻ¨ āĻāĻ°āĻ¤ā§ āĻšāĻā§āĻā§, āĻ¯āĻž āĻāĻĒā§āĻ¨-āĻā§āĻ˛ā§āĻāĻĄ āĻĒā§āĻ°āĻŋāĻ¨ā§āĻ¸āĻŋāĻĒāĻ˛ā§āĻ° āĻŦāĻŋāĻ°ā§āĻĻā§āĻ§ā§ āĻ¯āĻžā§āĨ¤
- āĻ¯āĻĻāĻŋ āĻāĻŽāĻ°āĻž āĻŦāĻžāĻĄāĻŧāĻŋāĻ° āĻŦā§āĻŽāĻžāĻ āĻ¸āĻŽāĻ°ā§āĻĨāĻ¨ āĻāĻ°āĻ¤ā§ āĻāĻžāĻ, āĻ¤āĻžāĻšāĻ˛ā§ āĻāĻŦāĻžāĻ° āĻā§āĻĄ āĻĒāĻ°āĻŋāĻŦāĻ°ā§āĻ¤āĻ¨ āĻāĻ°āĻ¤ā§ āĻšāĻŦā§āĨ¤
āĻĄāĻŋāĻāĻžāĻāĻ¨ āĻĒā§āĻ¨āĻ°ā§āĻāĻ āĻ¨
āĻāĻŽāĻ°āĻž āĻāĻŽāĻžāĻĻā§āĻ° āĻĄāĻŋāĻāĻžāĻāĻ¨āĻāĻŋ āĻĒā§āĻ¨āĻ°ā§āĻāĻ āĻ¨ āĻāĻ°āĻŋ:
- CustomerProfile āĻ¨āĻžāĻŽā§ āĻāĻāĻāĻŋ āĻ¨āĻ¤ā§āĻ¨ āĻāĻ¨ā§āĻāĻžāĻ°āĻĢā§āĻ¸ āĻ¤ā§āĻ°āĻŋ āĻāĻ°āĻŋāĨ¤
- āĻāĻ āĻāĻ¨ā§āĻāĻžāĻ°āĻĢā§āĻ¸ā§ āĻāĻāĻāĻŋ āĻŽāĻžāĻ¤ā§āĻ° āĻĒāĻĻā§āĻ§āĻ¤āĻŋ āĻĨāĻžāĻāĻŦā§:
isLoyalCustomer()
āĨ¤ - āĻāĻāĻ¯āĻŧ HealthInsuranceCustomerProfile āĻāĻŦāĻ VehicleInsuranceCustomerProfile āĻā§āĻ˛āĻžāĻ¸ āĻāĻ āĻ¸āĻžāĻ§āĻžāĻ°āĻŖ āĻāĻ¨ā§āĻāĻžāĻ°āĻĢā§āĻ¸āĻāĻŋ āĻŦāĻžāĻ¸ā§āĻ¤āĻŦāĻžāĻ¯āĻŧāĻ¨ āĻāĻ°āĻŦā§āĨ¤
interface CustomerProfile {
boolean isLoyalCustomer();
}
class HealthInsuranceCustomerProfile implements CustomerProfile {
public boolean isLoyalCustomer() {
// Logic for health insurance loyalty
}
}
class VehicleInsuranceCustomerProfile implements CustomerProfile {
public boolean isLoyalCustomer() {
// Logic for vehicle insurance loyalty
}
}
āĻ¨āĻ¤ā§āĻ¨ āĻā§āĻ¯āĻžāĻ˛āĻā§āĻ˛ā§āĻāĻ° āĻā§āĻ˛āĻžāĻ¸
āĻāĻāĻ¨ InsurancePremiumDiscountCalculator āĻā§āĻ˛āĻžāĻ¸ā§āĻ° āĻĒāĻĻā§āĻ§āĻ¤āĻŋ āĻĒāĻ°āĻŋāĻŦāĻ°ā§āĻ¤āĻ¨ āĻāĻ°āĻŋ:
class InsurancePremiumDiscountCalculator {
public double calculatePremiumDiscountPercent(CustomerProfile profile) {
if (profile.isLoyalCustomer()) {
return 10.0; // Loyal customer discount
}
return 0.0; // No discount
}
}
āĻ¸ā§āĻŦāĻŋāĻ§āĻž
- āĻāĻāĻ¨ āĻāĻŽāĻ°āĻž āĻ¯āĻĻāĻŋ āĻŦāĻžāĻĄāĻŧāĻŋāĻ° āĻŦā§āĻŽāĻžāĻ° āĻāĻ¨ā§āĻ¯ āĻāĻāĻāĻŋ āĻ¨āĻ¤ā§āĻ¨ āĻā§āĻ˛āĻžāĻ¸ āĻ¤ā§āĻ°āĻŋ āĻāĻ°āĻŋ, āĻ¯ā§āĻŽāĻ¨ HomeInsuranceCustomerProfile, āĻāĻāĻŋ āĻļā§āĻ§ā§āĻŽāĻžāĻ¤ā§āĻ° CustomerProfile āĻāĻ¨ā§āĻāĻžāĻ°āĻĢā§āĻ¸āĻāĻŋ āĻŦāĻžāĻ¸ā§āĻ¤āĻŦāĻžāĻ¯āĻŧāĻ¨ āĻāĻ°āĻŦā§āĨ¤
- āĻāĻŽāĻžāĻĻā§āĻ° InsurancePremiumDiscountCalculator āĻā§āĻ˛āĻžāĻ¸ā§ āĻā§āĻ¨ā§ āĻĒāĻ°āĻŋāĻŦāĻ°ā§āĻ¤āĻ¨ āĻāĻ°āĻ¤ā§ āĻšāĻŦā§ āĻ¨āĻžāĨ¤
āĻāĻĒāĻ¸āĻāĻšāĻžāĻ°
āĻāĻŽāĻ°āĻž āĻĻā§āĻā§āĻāĻŋ āĻāĻŋāĻāĻžāĻŦā§ āĻĄāĻŋāĻāĻžāĻāĻ¨ āĻĒā§āĻ¨āĻ°ā§āĻāĻ āĻ¨ āĻāĻ°ā§ āĻāĻĒā§āĻ¨-āĻā§āĻ˛ā§āĻāĻĄ āĻĒā§āĻ°āĻŋāĻ¨ā§āĻ¸āĻŋāĻĒāĻ˛ āĻ āĻ¨ā§āĻ¯āĻžāĻ¯āĻŧā§ āĻāĻžāĻ āĻāĻ°āĻž āĻ¯āĻžāĻ¯āĻŧāĨ¤ āĻĒā§āĻ°āĻĨāĻŽā§ āĻĄāĻŋāĻāĻžāĻāĻ¨āĻāĻŋ OCP āĻ āĻ¨ā§āĻ¸āĻ°āĻŖ āĻāĻ°āĻāĻŋāĻ˛ āĻ¨āĻž, āĻāĻŋāĻ¨ā§āĻ¤ā§ āĻĒā§āĻ¨āĻ°ā§āĻāĻ āĻ¨ā§āĻ° āĻŽāĻžāĻ§ā§āĻ¯āĻŽā§ āĻāĻāĻŋ OCP-āĻ° āĻ¸āĻžāĻĨā§ āĻ¸āĻžāĻŽāĻā§āĻāĻ¸ā§āĻ¯āĻĒā§āĻ°ā§āĻŖ āĻšāĻ¯āĻŧā§ āĻāĻ ā§āĻā§āĨ¤
āĻāĻāĻ¨ āĻĄāĻŋāĻāĻžāĻāĻ¨āĻāĻŋ āĻāĻŦāĻŋāĻˇā§āĻ¯āĻ¤ā§āĻ° āĻāĻā§āĻ¸āĻā§āĻ¨āĻļāĻ¨ā§āĻ° āĻāĻ¨ā§āĻ¯ āĻāĻ°āĻ āĻļāĻā§āĻ¤āĻŋāĻļāĻžāĻ˛ā§ āĻāĻŦāĻ āĻāĻžāĻ°ā§āĻ¯āĻāĻ°ā§āĨ¤
āĻāĻāĻŋ āĻāĻŋāĻ˛ āĻāĻĒā§āĻ¨-āĻā§āĻ˛ā§āĻāĻĄ āĻĒā§āĻ°āĻŋāĻ¨ā§āĻ¸āĻŋāĻĒāĻ˛ā§āĻ° āĻāĻāĻāĻŋ āĻāĻĻāĻžāĻšāĻ°āĻŖāĨ¤
đĨ02C-Key Takeaways from the Open-Closed Principle Code Example
Introduction
We will summarize the key takeaways from the previous code example that illustrated the Open-Closed Principle (OCP).
Principal Benefits of the New Design
-
Ease of Adding New Features
- The primary benefit of adhering to the Open-Closed Principle is the ease with which new features can be added to the system.
- This ease translates into cost savings for development and testing.
Cost Savings Explained
- If we do not follow OCP, adding new features often requires modifying existing code.
- Each modification increases the time spent on testing and quality assurance to ensure that no new bugs are introduced into the existing codebase.
- In contrast, when following OCP, testing new code is simpler and less time-consuming than running a full regression test suite on existing code.
- This principle is well-known among QA testers, who emphasize its importance in maintaining software quality.
Additional Benefits
-
Decoupling of Components
- By redesigning our system to conform to OCP, we inadvertently achieved a higher degree of loose coupling between components.
- This decoupling aligns with the Single Responsibility Principle (SRP), as each component now has a clearer purpose and responsibility.
Interconnectedness of SOLID Principles
- It is crucial to understand that the SOLID principles are intertwined and interdependent.
- They are most effective when applied together, providing a holistic approach to software design.
Cautionary Note
-
Do Not Follow OCP Blindly:
- While OCP is beneficial, blindly applying it can lead to an excessive number of classes, complicating your overall design.
- For instance, if you need to fix a bug and believe that modifying existing code is necessary for an effective fix, do so without hesitation.
- Avoid overhauling your design solely for bug fixes unless you notice recurring issues that could be mitigated by a redesign.
Subjective Application of OCP
- Deciding when and where to apply the Open-Closed Principle is subjective rather than objective.
- Use your judgment based on the specific context of your project.
Conclusion
- Re-designed code to make it follow OCP
- Following OCP can lead to cost benifits in the long run.
- OPC and SRP can work together to achieve a better design.
- Do not apply the OCPblindly and introduce unwanted complexity to your code.
The lessons learned from our code example related to the Open-Closed Principle. Understanding these key takeaways will help you apply OCP effectively in your future designs.
Top comments (0)