This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

The open closed principle

Proposed by Bertrand Meyer, in his 1988 book Object Oriented Software Construction: Software entities should be open for extension, but closed for modification. This is the classic definition of the open closed principle.

Open Closed Principie, OCP

Software entities should not modify the entity, but extend it.

For example, there is a financial reporting system that has implemented financial data rolling, and negative numbers are shown in red. However, the staff needs to print the report data and have other format requirements for the report data. If you want to meet this requirement, you need to think about whether you need to write new code, and how much you need to change old code.

It is implemented through the single principle SRP, which divides the calculation of financial data, data display, and print data into different responsibilities. After separation, the dependencies of the source code need to be confirmed to ensure that changes do not affect other components, and then the implementation of new functionality can be implemented through the OCP principle, without modification, but by extending the design.

Implementation method

The open and close principle is realized by “abstracting constraints and encapsulating changes”. Use abstract classes or interfaces to define a stable abstraction layer for entities, while variable factors are encapsulated in concrete implementation classes.

Therefore, when the software changes, it only needs to derive an implementation class to expand according to the requirements, strictly following the open closed principle.

Abstract Class Subject {display():void {}} class Subject1 {display() {console.log(' Concrete class 1 implementation... ')}} Class Subject12{display() {console.log(' Concrete class 2 implementation... ')}}Copy the code

Advantages of the open close principle

  • Impact on software testing: THE OCP principle is to extend the design, so the original function remains unchanged, so software testing can only test the extension part
  • Improve code reusability: The smaller the granularity, the more likely it is to be reused
  • Provides software maintainability: The open and closed principle makes software more stable and continuous, which makes it easier to expand and maintain

Disadvantages of the open close principle

  • Too many implementation classes can easily lead to too many maintenance of different concrete classes
  • Easy to lead to derivative overdesign

The scene is introduced

Familiar scenarios are: plug-ins and middleware. Designers must follow the open – close principle when developing the extension capability of plug-ins and middleware to users.

Therefore, the goal of the open closed principle is to make the system easy to expand without major changes. This goal is achieved by dividing the system into components, planning the dependency hierarchy of components, and protecting the upper level from the influence of the lower level components.

References:

  • C.biancheng.net/view/1322.h…
  • Wonderwater. Gitbooks. IO/clean_archi…