C++ Design Patterns: Null Object Pattern

empty object pattern

In the Null Object Pattern, an empty object replaces the check for a NULL object instance. Instead of checking for null values, the Null object reflects a relation that does nothing. Such Null objects can also provide default behavior when data is not available.

In the empty object pattern, we create an abstract class that specifies various operations to be performed and a concrete class that extends this class, and also create an empty object class that does not implement any of the class, which will seamlessly Use where you need to check for null values.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

scenes to be used

Avoid judging the null value that frequently appears in the program.

Advantages and disadvantages

advantage:

There is no special handling to check for null values ​​when using objects.

shortcoming:

1. It is not known that it is a null value when using it. If the interface class needs to use exceptions, etc., it cannot be used.

2. Added classes, increasing the complexity of structure and hierarchy.

Precautions

If you just want to avoid the appearance of null, then please don't use the empty object mode.

UML structure diagram

 

Code

interface.h
creates abstract class - customer; creates entity class - real customer, empty customer

#include <string>
#include <iostream>
using namespace std;

class AbstractCustomer  //基类-抽象客户
{
public:
    AbstractCustomer() {}
    virtual ~AbstractCustomer() {}

    virtual bool isNull() = 0;
    virtual string getName() = 0;

protected:
    string name;
};

class RealCustomer: public AbstractCustomer //子类-真实客户
{
public:
    RealCustomer(string name)
    {
        this->name = name;
    }

    string getName() { return this->name; }

    bool isNull() { return false; }
};

class NullCustomer: public AbstractCustomer //子类-空客户
{
public:
    string getName() { return "Not Available in Customer Database"; 

    bool isNull() { return true; }
};

customerfactory.h
creates a customer, if it exists, create a real customer, if it does not exist, create an empty customer

#include "interface.h"
#include <vector>

class CustomerFactory
{
public:
    std::vector<string> vec;

    AbstractCustomer * getCustomer(string name) {
        for (auto it : vec)
        {
            if (it == name)
            {
                return new RealCustomer(name);
            }
        }

        return new NullCustomer();
    }
};

main.cpp
example application - replace null with empty object

#include "customerfactory.h"

int main()
{
    CustomerFactory customerFactory;

    customerFactory.vec.push_back("Billy");
    customerFactory.vec.push_back("Kitty");
    customerFactory.vec.push_back("Alice");
    customerFactory.vec.push_back("Miss");

    AbstractCustomer *customer1 = customerFactory.getCustomer("Billy");
    AbstractCustomer *customer2 = customerFactory.getCustomer("Kitty");
    AbstractCustomer *customer3 = customerFactory.getCustomer("Alice");
    AbstractCustomer *customer4 = customerFactory.getCustomer("Miss");
    AbstractCustomer *customer5 = customerFactory.getCustomer("Jason");

    cout << "Customers:" << endl;
    cout << customer1->getName() << endl;
    cout << customer2->getName() << endl;
    cout << customer3->getName() << endl;
    cout << customer4->getName() << endl;
    cout << customer5->getName() << endl;

    return 0;
}

运行结果:
Customers:
Billy
Kitty
Alice
Miss
Not Available in Customer Database

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/129767020