Notes on the principle of opening and closing of C++ design patterns

Table of contents

1. What is the open-closed principle

2. Why there is an open and closed principle

3. And how to use the principle of opening and closing


1. What is the open-closed principle

Definition: A software entity such as classes, modules and functions should be open for extension and closed for modification.

To put it bluntly, when a product is ready, if there are new functions added, we should try to expand instead of modifying the already designed functions or structures, such as laptops, the manufacturer produces a notebook, if we want to use For the mouse, we only need to use the interface given to us by the manufacturer to connect to the usb that we bought or made.

2. Why there is an open and closed principle

The customer asked to design a login interface requirement

loginFrom is the login interface
and Cirlebutton is a circular button in the interface
, so I quickly designed it, so I sent the designed class to the project team leader.
The team leader looked at me for two minutes and clenched his fists. Do it, and call back at last.
I don’t know if I haven’t learned design patterns, so
the following is the code I wrote

class Cirlebutton {
public:
    void dispaly() {
        ......
    }
};
class loginFrom {
    Cirlebutton btA;
public:
    void displays() {
        ........
    }
};

It can be seen that although I have realized the basic requirements, if the requirements change and I want to change a new square button, do I have to mechanically change the Cirlebutton in loginfrom to RectangeButton?
Do I have to change it back every time the button changes, so the scalability of the program is extremely poor, and it is also very unfriendly to other people, so
I learned the design pattern today and spent some time changing the code, and submitted it carefully Code, and then the arrogant team leader looked happy.

class AbstractButton {
public:
    virtual    void displays() {

      .........
    };
};
class Cirlebutton : public AbstractButton {
public:
    void displays() {
    ............
    }
};
class loginFrom {
    AbstractButton* btA;
public:
    void setbt(AbstractButton* btAa) {
        this->btA = btAa;
    };
    void displays() {
        this->btA->displays();
    }

};

So what is the mystery of this proxy purchase?
You can see that this code adds an abstract parent class AbstractButton as an interface, and
the subclass Cirlebutton implements AbstractButton. At the same time, it rewrites the display method
and adds a new keyword virtual to the parent class to modify the display abstraction. method

3. And how to use the principle of opening and closing

In Java, we can define interface-oriented programming. We only need the implement method
, but there is no such keyword in C++. C++ can implement interfaces by defining virtual functions or pure virtual functions through the keyword virtual, and then subclasses inherit this interface. Class and define the function of the virtual function,
so that every time we change the business requirements, we only need to define a new button to inherit

Guess you like

Origin blog.csdn.net/m0_59054762/article/details/130460752
Recommended