![]() |
VOOZH | about |
The Null Object Design Pattern is a behavioral design pattern that is used to provide a consistent way of handling null or non-existing objects. It is particularly useful in situations where you want to avoid explicit null checks and provide a default behavior for objects that may not exist.
Important Topics for the Null Object Design Pattern
The Null object pattern is a design pattern that simplifies the use of dependencies that can be undefined. This is achieved by using instances of a concrete class that implements a known interface, instead of null references. We create an abstract class specifying various operations to be done, concrete classes extending this class, and a null object class providing do-nothing implementation of this class which will be used seamlessly where we need to check the null value.
The client is the code that depends on an object that implements a Dependency interface or extends a DependencyBase abstract class. The client uses this object to perform some operation. The client should be able to treat both real and null objects uniformly, without needing to know which type of object it is dealing with.
DependencyBase is an abstract class or interface that declares the methods that all concrete dependencies, including the null object, must implement. This class defines the contract that all dependencies must adhere to.
This class is a functional dependency that may be used by the Client. The client interacts with Dependency objects without needing to know whether they are real or null objects.
This is the null object class that can be used as a dependency by the Client. It contains no functionality but implements all of the members defined by the DependencyBase abstract class. NullObject represents a null or non-existing dependency in the system. The client can safely call methods on a NullObject without causing errors or needing to perform null checks.
Problem Statement:
Consider a car rental service where customers can choose to rent different types of cars.
Car that defines the behavior of a car, such as drive() and stop() methods.Car interface, such as Sedan, SUV, and Convertible. These represent real cars that customers can rent.null reference or raising an error, the rental service could provide a NullCar object that implements the Car interface but doesn't actually represent any specific car model. This way, the customer can still use the drive() and stop() methods without causing issues.NullCar object, ensuring that the customer can still drive and stop the car, even though it doesn't correspond to a real car modelA car rental service allows customers to rent different types of cars. However, some customers may request a car model that is not available in the rental service's fleet. When this happens, the rental service needs a way to handle this situation gracefully without causing errors or disruptions to the customer.
The Null Object Design Pattern is helpful in this situation because it allows the rental service to provide a "null" car object that can be used in place of a real car object when the requested car model is not available. This way, the customer can still use the car object without encountering null reference exceptions or other errors.
👁 nullobjectdesignpatterbBelow is the complete code of the above example:
Car interface defines the behavior of cars with drive() and stop() methods.SUV and Sedan classes are real implementations of the Car interface, representing actual cars that can be rented.NullCar class is a null object implementation of the Car interface, providing default or no-op implementations of drive() and stop() methods.CarRentalService class is a client that interacts with the Car objects. It has a method rentCar() that calls drive() and stop() on the Car object, regardless of whether it is a real car or a null car.The Null Object Design Pattern is useful in situations where you want to provide a default or no-op implementation of an object's behavior to avoid null checks and handle null references gracefully. Here are some scenarios where the Null Object Design Pattern can be beneficial:
There are some cases where the Null Object Design Pattern may not be suitable: