Design pattern principle

Six principles
1. Opening and closing principle: open to expansion and close to modification. When the program is expanded, it is realized by adding code without modifying the original code.
For example, if you want to write an addition calculator, you can define a class to input two numbers and symbols, but if you want to add new operations, you need to modify the internal code.
Therefore, it is necessary to adopt polymorphism, and each operation inherits the same parent class (Interface), so that if you want to add functions, you only need to inherit the parent class and rewrite a class to achieve the purpose of expansion.

#include <iostream>
using namespace std;
//Interface class, which can define multiple interface classes and use the interface isolation principle
class caculation{
public:
    virtual void setPara(int a,int b)=0;
    virtual int getResult() = 0;
};

class pluscaCulation:public caculation{
public:
    virtual void setPara(int a,int b){
        this->mA = a;
        this->mB = b;
    }
    virtual int getResult(){
        return mA+mB;
    }
private:
    int mA;
    int mB;
};

class multcaCulation:public caculation{
public:
    virtual void setPara(int a,int b){
        this->mA = a;
        this->mB = b;
    }
    virtual int getResult(){
        return mA*mB;
    }
private:
    int mA;
    int mB;
};
void test01(){
    caculation *cal = new pluscaCulation;
    cal->setPara(10,20);
    cout<<cal->getResult()<<endl;
    delete cal;
};
int main() {
    test01();
    return 0;
}

2. Dimiter's Law:
Design a data system, and then another to call, can not directly provide this system, it is better to design another class, as a mediation interface, to read the data system.

3. Richter's substitution principle: polymorphism, a parent class pointer can point to all child class objects. Any parent class can be replaced by a child class and is not affected.

4. Dependency Inversion Principle: do not over rely on multiple classes, that is, the input type of one class is better not to be another class, but to be the interface class of another class (that is, the parent class of multiple classes is the interface), so that all classes with the same interface can be input into this class.

5. Principle of composite reuse: try to use combination instead of inheritance

#include <iostream>
using namespace std;

class carType{
public:
    virtual void run() = 0;
};
class dazhong: public carType{
public:
    virtual void run(){
        cout<<"I am running dazhong car."<<endl;
    }
    ~dazhong(){
        cout<<"delete dazhong"<<endl;
    }
};
class tuolaji: public carType{
public:
    virtual void run(){
        cout<<"I am running tuolaji."<<endl;
    }
    ~tuolaji(){
        cout<<"delete tuolaji"<<endl;
    }
};
class person{
public:
    person(){
        cout<<"start"<<endl;
    }
    void setcar(carType *car){//In the combination used here, the input is a class.
        this->car = car;
    }
    void doufeng(){
        car->run();
    }
    ~person(){
        if(this->car!= nullptr){
            delete this->car;
        }
    }
private:
    carType *car;
};
void test01(){
    person *p = new person;
    p->setcar(new dazhong);
    p->doufeng();

    p->setcar(new tuolaji);
    p->doufeng();
    delete p;
}
int main() {
    test01();
    return 1;
}

Keywords: calculator

Added by Lol5916 on Thu, 28 Nov 2019 22:51:25 +0200