Skip to content

SOLID Principles

2025-03-27

What is object-oriented programming?

Design patterns are for the programming paradigm of object-oriented programming. So before learning design patterns, we must also understand what object-oriented programming is. ​​Object-oriented programming is to extract different objects from the problem, and each object has its own properties and behaviors. The process of solving problems is completed by the interaction between objects through "behavior".

What is the difference between object-oriented and process-oriented?

Corresponding to object-oriented programming (OOP), there is also process-oriented programming (POP). These two programming paradigms represent two ideas of code organization. We will take the example of writing a small program for playing Gobang to see how two different ideas will organize the same function of playing chess.

Process Oriented (POP)

For the chess game, we divide it into several steps: displaying the chessboard, playing chess, and checking victory. The function of playing chess is achieved by continuously looping several functions.

Please refer to the pseudo code below:

play_chess.py

Object-oriented (OOP)

In the process of playing chess, we found that we can extract Board and Player are two objects. Displaying, judging victory, judging whether the board is full, moving chess pieces, etc. are board actions. Playing chess is a player action. So we can get the following pseudo code:

play_chess.py

Advantages of object-oriented programming

  1. More clarity. You can manage the state and behavior of an object separately. Procedural programming often confuses the two.
  2. Code reuse. Code reuse can be achieved through inheritance and polymorphism. For example, now in addition to human players, we can add AI players. We can directly inherit without adding too many functions.
  3. High scalability. If the function of retracting chess is added at this time, object-oriented programming can be modified by adding classes or methods in classes without modifying existing parts.

Summary

This column will introduce 5 class design principles that need to be followed in the process of object-oriented programming, so as to help programmers better manage their own code and truly play the advantages of object-oriented programming.

The seven principles are not isolated from each other, and there is a certain relationship between them. One may be a reinforcement or foundation of the other, and violating one may mean violating several other principles at the same time. Among them, Open-Closed Principle is the cornerstone of object-oriented programming, and other principles can be regarded as means and tools to implement the Open-Closed Principle.

Design Goals

Design Methods

Keep It Simple