preface
To learn design patterns, you need to understand object-oriented ideas. Using the characteristics of object-oriented thinking can help us design excellent programs.
Process oriented thought
characteristic
- Focus: ongoing process
- Things happen in a certain "logical chronological order"
- In line with the logic of most people thinking and computer processing
Example (C + + language description)
#include <iostream> using namespace std; int main() { //Define and initialize variables int num1 = 1; //Operand 1 char oper = '+'; //Calculator int num2 = 2; //Operand 2 int result = 0; //Calculation results //Determine and calculate switch (oper) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; default: break; } cout << result << endl; //Output calculation results return 0; }
The program first defines and initializes variables, then makes judgment and calculation, and finally outputs the results. The execution of the program goes through a certain process "from top to bottom" (in the order of sentences) and "from the past to the future" (in the order of time).
shortcoming
- Poor maintainability
- Poor reusability
- Poor scalability
- Poor flexibility
Four characteristics of excellent programming
In the actual business and service development, the requirements change, and the program needs to be modified. Excellent program design aims to complete the task in an orderly and error free manner in a shorter time, less code changes and certain design steps.
- Maintainability: only modify relevant codes / functions without affecting other codes / functions
- Reusability: Code / function can be reused
- Expansibility: it can add new codes / functions on the basis of the original codes / functions
- Flexibility: the code / function is applicable to a variety of scenarios
Object oriented thought
Focus: objects being affected
Three characteristics: encapsulation, inheritance and polymorphism
encapsulation
objective
- Split logic
- Reduce coupling
- Hide implementation details
- ...
Example (C + + language description)
#include <iostream> using namespace std; //Calculation class class Operation { public: int num1; //Operand 1 char oper; //Calculator int num2; //Operand 2 int result; //Calculation results //Calculation method void calculate() { switch (oper) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; default: break; } } }; int main() { //Create and initialize the object of the calculation class Operation operation; operation.num1 = 1; operation.oper = '+'; operation.num2 = 2; operation.calculate(); //calculation cout << operation.result << endl; //Output calculation results return 0; }
The program defines a calculation class, which encapsulates the "entity" that logically resembles the "operand, operator, calculation result and calculation method" and jointly realizes a function; Split the calculated business logic and the code logic of the output display interface in the main function. You can choose to modify only one part of the code without affecting the other part, reducing the coupling degree; For the main function, the implementation details of the calculation class are hidden. The main function only needs to know how to call and can reuse the calculation class in other places.
Inheritance and polymorphism
objective
- Split logic
- Reduce coupling
- ...
When the calculation class needs to be modified, there is still the problem of high coupling. For example, if you want to add the "multiplication calculation" function to it, you need to add code in the "switch" statement of the "calculate()" function, which may wrongly modify the code of the "addition and subtraction calculation" function.
Example (C + + language description)
#include <iostream> using namespace std; //Calculation class class Operation { public: int num1; //Operand 1 char oper; //Calculator int num2; //Operand 2 int result; //Calculation results virtual void calculate() = 0; //Pure virtual function }; //Computational addition class class Operation_add : public Operation //inherit { void calculate() //Overload polymorphism { result = num1 + num2; } }; //Calculation subtraction class class Operation_sub : public Operation { void calculate() { result = num1 - num2; } }; //Computational multiplication class class Operation_mul : public Operation { void calculate() { result = num1 * num2; } }; /** * Program design idea: according to the given calculator, create the corresponding subclass object of the calculation class, and call the subclass method to calculate * Use simple factory mode */ //Calculation factory class class Operation_factory { public: static Operation *create_operation(char oper) //Static function { Operation *operation = NULL; //Pointer to the calculated class //According to the given calculator, create the corresponding subclass of the calculation class switch (oper) { case '+': operation = new Operation_add(); break; case '-': operation = new Operation_sub(); break; case '*': operation = new Operation_mul(); break; default: break; } return operation; //Returns the corresponding calculated subclass object } }; int main() { //According to the given calculator, the corresponding subclass object of the calculation class is created and initialized Operation *operation = Operation_factory::create_operation('+'); //Static functions belong to classes rather than objects and are called directly by classes operation->num1 = 1; operation->num2 = 2; operation->calculate(); //calculation cout << operation->result << endl; //Output calculation results return 0; }
Through inheritance, each "sub function" of "calculation" is logically split, and each function is separated into a "class", reducing the coupling degree; Through polymorphism, the function is overloaded to realize different calculations. When you want to add a "division calculation" function, you only need to add a calculation division subclass, and add a branch to create the calculation division subclass object in the "switch" statement of the calculation factory class without "touching" / mistakenly modifying the code to the "addition, subtraction and multiplication calculation" function, which improves the maintainability of the program.
summary
- Process oriented focus: the process that is happening
- Four characteristics of excellent programming: maintainability, reusability, expansibility and flexibility
- Object oriented concerns: objects being affected
- Three characteristics: encapsulation, inheritance and polymorphism
Author's words
- The content of this article is based on the reader's certain programming foundation and understanding of process-oriented, object-oriented thought and other certain knowledge. The main purpose is to talk about and extend knowledge. For detailed concept understanding, please refer to other materials
- The content of the article is simple, and the embodiment of the ideological superiority may not be ideal. When expanding to large businesses, services and procedures, we must have efficient design ideas and use good design patterns for programming
- If there are doubts in the description of the article, please leave a message and we will patiently discuss and answer them one by one
- There are mistakes in the article, please criticize and correct
- I hope readers can gain something
reference material
Dahua design pattern by Cheng Jie