Null Design Pattern is intentionally designed to reduce the NULL check. If there is data not available, then the pattern introduces a class with does nothing. Instead of putting if null check Null object is used to default behavior.
Null Object pattern is not only used for Null reference but also able to be used as the default behavior in case data is not available. Most of the language’s null references are problems with calling actions. Therefore null check is mandatory before calling actions. Otherwise, the code gets runtime exceptions. As a solution, the Null Design pattern returns a Null Object if data is not available.
There are introducing four components in this pattern. Those are
- Base Object: Interface or Abstract class defines the operation.
- Real Objects: Actual object that the client looking for. implemented Base Object operation as a Real Object
- Null Object: implemented Base Object operation but does nothing with the operation or does default behavior or run neutral behavior.Â
- Client: This is executing the Objects using Base Object reference.
Let’s try to find a real-world example with Null Design Pattern
Null Design Pattern Real World Example
Let’s consider in-office assigned work with each employer. Consider there are 5 employees in the office and 2 of them suddenly not in the office due to personal reasons or medical reasons. Then those 2 employees do not get any work and are not able to assign any work.
According to this example Nothing to do with absent employees. Let’s implement this simple example with a class diagram.
Null Design Pattern in Java
According to the example, there is nothing to worry about regarding Employee absence also this is not getting a Null (Null Pointer) Exception. Therefore Null Design pattern is safe to use in predictable Nullable Objects.
// Interface for define functions abstract class OfficeEmployee { protected String name; public abstract void assignWork(); } // Real Object and implemented interface class PresentEmployee extends OfficeEmployee { protected String name; public PresentEmployee(String name) { this.name = name; } public void assignWork(){ System.out.println(name+"'s work assined"); } } // Null Object just implemented but not functioning like actual class NullEmployee extends OfficeEmployee { protected String name; public NullEmployee(String name) { this.name = name; } public void assignWork(){System.out.println(name+"'s Not Available");} } // Create Real and Null object according to availability class EmployeeAttendance { private static final String[] names = {"John", "Jenny", "Micheal"}; public OfficeEmployee getEmployee(String name) { for (int i = 0; i < names.length; i++) { if (names[i].equalsIgnoreCase(name)) { return new PresentEmployee(name); } } return new NullEmployee(name); } } // Client that use Real and Null Objects public class NullEmployeeClient { public static void main(String[] args) { EmployeeAttendance ea= new EmployeeAttendance(); OfficeEmployee emp1 = ea.getEmployee("Jenny"); OfficeEmployee emp2 = ea.getEmployee("Martin"); OfficeEmployee emp3 = ea.getEmployee("John"); OfficeEmployee emp4 = ea.getEmployee("Anne"); emp1.assignWork(); emp2.assignWork(); emp3.assignWork(); emp4.assignWork(); } }
Output
Jenny's work assined Martin Not Available John's work assined Anne Not Available
Advantages of Null Design Pattern
- Reduce the Null Pointer Exception and its help to Reduce Runtime Exception
- Easy to create default behavior once an object is not present or visible at runtime
- Take default value or Null value once the original object is not available
Design Patterns Book
Design Patterns are one of the most famous solutions in programming and millions of people follow them to fix their tasks, design projects, and so on. Most of the patterns create based on basic OOP concepts (If your beginner then read our basic OOP, Encapsulation, Inheritance, Polymorphism, and Abstraction articles with great examples )
There are a lot of articles and design pattern books available with different examples to read. Other than our article if you are interested in this topic you could go through the below books as well.
Books | Description |
---|---|
Head First Design Patterns: A Brain-Friendly Guide | My favorite and it’s more readable. There are good examples and they look good to beginners as well. |
Design Patterns: Elements of Reusable Object-Oriented Software | There are 23 design patterns. showing how to select an appropriate pattern for your case. |
Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software | There are a lot of simple examples showing how to use design patterns in the correct way. Those are based on SOLIC principles. |
There are a lot of design pattern ebooks on Amazon. Those design patterns are written with great examples in a different programming language. I included a few of them according to their best ranking.