Agile Software Development# 9.OCP: The Open-Closed Principle

This post is my reading note on Agile Software Development by Robert C. Martin.

9.OCP: The Open–Closed Principle

 

Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.

 

📌What is Open-Closed Principle?

Open: open for extension

Closed: closed for modification

 

📌Example of not OCP

image-20220203170027372

Because Client and Server classes are concrete.

 

📌Abstraction is the KEY

1️⃣OCP is NOT one-off[^5]. Because we meet changes, and changes will affect the abstraction for current OCP.

2️⃣There is no abstraction that is natural to all contexts! (still due to changes)

3️⃣Don't over-"abstract" the design(Needless complexity).But, we wait until the changes happen!

4️⃣"Fool me once, shame on you. Fool me twice, shame on me." To keep from loading our software with Needless Complexity, we may permit ourselves to be fooled once.

5️⃣Resisting premature abstraction is as important as abstraction itself.⭐

 

📌Primary Mechanism behind OCP

Speak without particular programming language

  • abstraction
  • polymorphism

Speak with particular programming language (e.g. C++)

  • inheritance = abstraction + polymorphism

 

 

📌STRATEGT pattern on OCP

image-20220203170336287

The preceding is the diagram after applied OCP. One question left, why did we abstract Client as ClientInterface rather than AbstractServer?

Because abstract classes are more closely associated to their clients[^4] than to the classes that implement them.

 

📌TEMPLATE METHOD pattern on OCP

image-20220203175155878

The policy functions describe some work that needs to be done in terms of some abstract interfaces. e.g. pure virtual functions in C++.

 

📌Bad Practice violating OCP

In the preceding example, the switch-case block is violating the OCP. In the future, it requires lots of coding when modification comes in, e.g. triangle.

 

📌Using polymorphism for OCP

Detail code refers to here.

In the future, if I were asked to draw triangle, I can easily add a Triangle::Shape class without touching other codes.

Previous
Previous

Agile Software Development# 10.LSP: The Liskov Substitution Principle

Next
Next

Agile Software Development# 8.SRP: The Single-Responsibility Principle