Cognitive Scaffold

Preparing your thinking workspace

arrow_back_ios_new
MENTAL MODEL · M1239

Open/Closed Principle

Open/Closed Principle
CreateHigh supportDesign Thinking
Included
account_tree

Updated 2026-07-30

Loading revision record…

Summary

In the field of object-oriented programming, the Open/Closed Principle (OCP) states that "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification," meaning an entity is allowed to change its behavior without altering its source code. This feature is particularly valuable in production environments where changing source code requires code reviews, unit tests, and similar processes to ensure product quality. Code that follows this principle does not change when extended, thus avoiding these processes. The naming of the Open/Closed Principle is applied in two ways. Both use inheritance to resolve the apparent dilemma, but their purposes, techniques, and results are different.

Definition

Software entities (classes, modules, functions) should be open for extension but closed for modification. That is, new functionality is introduced by adding new code without modifying existing code. Scaffold use: Architecture that embraces change. When designing rules or processes, reserve "plugin interfaces." When new situations arise, there is no need to start over (modification); just plug in a new plugin (extension).

How to use

Architecture that embraces change. When designing rules or processes, reserve "plugin interfaces." When new situations arise, there is no need to start over (modification); just plug in a new plugin (extension).

Mechanism

The Open/Closed Principle (OCP) requires that software entities be open for extension and closed for modification: new behaviors should be added through extension rather than by altering existing code, thereby reducing regression risk and improving stability.

Practice

In progress 0/4

Practice this model in one real situation:

Check to track your progress (stored locally)
Learning progress0%

Cases

  • Use the strategy pattern to add new algorithms without changing the original class; plugin architecture supports extension.

Common pitfalls

  • Over-abstraction leads to class explosion; pre-building unnecessary complexity for 'possible changes'.
Sources

Source support: Explicit

References currently contain only Wikipedia links; primary sources still need review.

  • link
    zh.wikipedia.orghttps://zh.wikipedia.org/wiki/%E5%BC%80%E9%97%AD%E5%8E%9F%E5%88%99ZH · Explicit
    verified
account_treeGenealogyexpand_more

RELATED MODELS