·
The null object pattern falls under Behavior
design pattern
·
The object is null but your code can still run.
·
The use of Null Object pattern simplifies the
client code and makes it less error prone.
·
The use of the Null Object pattern is that it
doesn't require a null check to prevent a crash or error.
·
Consider an application which does orders based
on the discount.
Below is the Order class:
[Code]
Let us say we have two types of discount “Premium discount “and “Festival
discount”
[Code]
Now we can able to create Premium and Festival orders using Premium and
Festival discount
Now let’s take an example where we need to create an order for customer
who had no discount.
[Code]
Now the applications crashes
[Image]
We need to create a discount class which does NO discount calculation as
shown in the below code.
[Code]
Now the below client call to order object would
not crash.
[Code]
Reference : https://www.codeproject.com/Articles/1042674/NULL-Object-Design-Pattern
No comments:
Post a Comment