In object-oriented programming, the open–closed principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification” that is, such an entity can allow its behavior to be extended without modifying its source code.
Wiki – Open closed principle
The open-closed principle (OCP) is one of the SOLID principles which states the entities(classes or methods) should be open for extension but closed for modification. The “O” in “SOLID” represents the open-closed principle. Many developers descript the OCP of the software, then can find out the developer’s description has two versions. If you like to read more books about software or the research paper on computer science, you will know these versions which are Meyer’s open-closed principle and Polymorphic open-closed principle.
The open-closed principle idea started from Object-Oriented Software Construction by Meyer, Bertrand(1988). Meyer describes the open-closed principle below:
In Meyer’s book, adding fields or functions to a library inevitably required change to any programs depending on that library Meyer provides the solution to this dilemma relied on the notion of object-oriented inheritance (specifically implementation inheritance).
It is not many developers can accept it. Around ten years, a part of computer science researcher proposes another method to descript the OCP and continuously explain the OCP. During the 1990s, the open-closed principle became popularly redefined to refer to the use of abstracted interfaces, where the implementations can be changed and multiple implementations could be created and polymorphically substituted for each other. It is called the “Polymorphic open-closed principle”.
This definition advocates inheritance from abstract base classes which contrasts with Meyer’s usages. The interface object specifications can be reused through inheritance, but the implementation doesn’t need to be. The existing interface object is closed to modifications and new implementations must, at a minimum, implement that interface.
In the modern age, many developers or interview questions answer will prepare to accept the polymorphic open-closed principle; then there think it will be complete answers. May a part of senior developers only know Meyer’s open-closed principle and don’t know the polymorphic open-closed principle or on contrary.
How to implement the open-closed principle (OCP) in the program?
Basic part:
internal class DeveloperReport
{
public int Id { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public string Level { get; set; } = string.Empty;
public int WorkingHours { get; set; } = 0;
public double HourlyRate { get; set; } = 0;
}
public abstract class BaseSalarycalculator
{
protected DeveloperReport DeveloperReport { get; private set; }
public BaseSalarycalculator(DeveloperReport developerReport)
{
DeveloperReport = developerReport;
}
public abstract double CalculateSalary();
}
public class SeniorDevSalaryCalculator : BaseSalarycalculator
{
public SeniorDevSalaryCalculator(DeveloperReport report) : base(report)
{ }
public override double CalculateSalary()
=> DeveloperReport.HourlyRate * DeveloperReport.WorkingHours * 1.2;
}
public class JuniorDevSalaryCalculator : BaseSalarycalculator
{
public JuniorDevSalaryCalculator(DeveloperReport report) : base(report)
{ }
public override double CalculateSalary()
=> DeveloperReport.HourlyRate * DeveloperReport.WorkingHours;
}
public class SalaryCalculatorPro
{
private readonly IEnumerable<BaseSalarycalculator> _developerCalculation;
public SalaryCalculatorPro(IEnumerable<BaseSalarycalculator> developerCalculation)
{
_developerCalculation = developerCalculation;
}
public double CalculateTotalSalaries()
{
double totalSalaries = 0D;
foreach (var devCalc in _developerCalculation)
{
totalSalaries += devCalc.CalculateSalary();
}
return totalSalaries;
}
} Advanced part:
internal class DeveloperReport
{
public int Id { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public string Level { get; set; } = string.Empty;
public int WorkingHours { get; set; } = 0;
public double HourlyRate { get; set; } = 0;
}
public interface IOCP : IDisposable
{
abstract double CalculateSalary();
}
public abstract class BaseSalarycalculator : IOCP
{
protected DeveloperReport DeveloperReport { get; private set; }
public BaseSalarycalculator(DeveloperReport developerReport)
{
DeveloperReport = developerReport;
}
public abstract double CalculateSalary();
public void Dispose() => GC.SuppressFinalize(this);
}
public class SeniorDevSalaryCalculator : BaseSalarycalculator
{
public SeniorDevSalaryCalculator(DeveloperReport report) : base(report)
{ }
public override double CalculateSalary()
=> DeveloperReport.HourlyRate * DeveloperReport.WorkingHours * 1.2;
}
public class JuniorDevSalaryCalculator : BaseSalarycalculator
{
public JuniorDevSalaryCalculator(DeveloperReport report) : base(report)
{ }
public override double CalculateSalary()
=> DeveloperReport.HourlyRate * DeveloperReport.WorkingHours;
}
public class SalaryCalculatorPro
{
private readonly IEnumerable<BaseSalarycalculator> _developerCalculation;
public SalaryCalculatorPro(IEnumerable<BaseSalarycalculator> developerCalculation)
{
_developerCalculation = developerCalculation;
}
public double CalculateTotalSalaries()
{
double totalSalaries = 0D;
foreach (var devCalc in _developerCalculation)
{
totalSalaries += devCalc.CalculateSalary();
}
return totalSalaries;
}
} Reference
What is the API gateway? An API gateway is an API management tool that sits…
The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…
In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…
In the field of software engineering, the interface segregation principle (ISP) states that no code…
Subtype Requirement: Let be a property provable about objects of type T. Then should be true for objects of type S where S is…
The single-responsibility principle (SRP) is a computer-programming principle that states that every module, class or…