Null Object Pattern

Null Object Pattern is a #Design Pattern where an object explicit neutral behaviour and/or default values. It is usually useful to introduce it by extends (or inherit) from an object that could have a null case. Instead of relying on checking whether the object is null or not, we can create a Null Object and check its behaviour. It simplifies the codebase and apparently a great tool for #Refactoring.

An example of implementation of it using #cpp is presented as follows:

class Animal {
  public:
    bool isNull(void) {
      return false;
    }
};

class NullAnimal : public Animal {  // NullAnimal extends Animal
  public:
    bool isNull(void) override {
      return true;
    }
}
Links to this page
#oop #cpp