filmov
tv
Null Object Pattern - API Tutorial
Показать описание
Null Object Pattern is a free tutorial by Engineer Spock from API course
Link to this course(Special Discount):
This is the best API Course
Course summary:
Design and implement a type or an API taking care of it's users.
Encapsulate types so the other programmers would not hate you.
Code in a good style making the code clearer in its intent.
Refactor the code making it much better to read and understand.
Throw and handle exceptions properly.
Decide whether to comment a particular part of the code is a good idea or not. By the way, which comments are helpful and which are not?
Dealing with Null values
English
There are several practices for fighting with nulls. One of them is the time-honored Null Object pattern. This is one of the easiest ways to represent nulls. The Null Object pattern is about creating a special case for each type in order to represent its null state. Here you can see the diagram which represents the design of this pattern. We can imagine almost any class as real entity which potentially can be null. Our goal is to eliminate nulls when working with this reference type. In order to do that we need to extract a base class which is represented on this diagram as an Abstract Entity. Please notice that we extracted the API of the real entity which is represented by a method named �DoSomething�. After that we can roll out a NullEntity which inherits from that base class. The implementation of the doSomething method will be empty in this case. So, when a method needs a real entity it should require an abstract entity to be passed in as an argument. If other methods which return abstract entity need to return a null value, they will return a null entity, so, if throughout the system we use Null Entities instead of nulls, then we will not get null reference exceptions. Let�s consider an example.
Let�s pretend that we build some kind of a caching system. Here, we define the ICacheStorage interface with three members: Remove, Store, and Retrieve. One of the useful implementations would be the HttpContextCacheStorage which caches data in the HttpContext. And here is the NullObjectCache which implements the same caching interface. What this implementation does? It literally does nothing.
Look at the possible client�s code. What might have happened if the argument of the PerformWork method would have been null? NullReferenceException would have been thrown from the attempt to retrieve an object. Now consider what will happen if we pass an instance of the NullObjectCache class. Nothing. Literally nothing. Sometimes this is a really desired behavior and in such cases, this pattern provides a safety net. So, remember, this pattern is useful when you know that there is a meaningful default behavior. �doing nothing� is a meaningful behavior in certain cases. If this is inappropriate for some reasons, then it might be better to stick with nulls or implement other strategies of avoiding nulls.
Null Object Pattern Learn the Null Object pattern in�C#.
Link to this course(Special Discount):
This is the best API Course
Course summary:
Design and implement a type or an API taking care of it's users.
Encapsulate types so the other programmers would not hate you.
Code in a good style making the code clearer in its intent.
Refactor the code making it much better to read and understand.
Throw and handle exceptions properly.
Decide whether to comment a particular part of the code is a good idea or not. By the way, which comments are helpful and which are not?
Dealing with Null values
English
There are several practices for fighting with nulls. One of them is the time-honored Null Object pattern. This is one of the easiest ways to represent nulls. The Null Object pattern is about creating a special case for each type in order to represent its null state. Here you can see the diagram which represents the design of this pattern. We can imagine almost any class as real entity which potentially can be null. Our goal is to eliminate nulls when working with this reference type. In order to do that we need to extract a base class which is represented on this diagram as an Abstract Entity. Please notice that we extracted the API of the real entity which is represented by a method named �DoSomething�. After that we can roll out a NullEntity which inherits from that base class. The implementation of the doSomething method will be empty in this case. So, when a method needs a real entity it should require an abstract entity to be passed in as an argument. If other methods which return abstract entity need to return a null value, they will return a null entity, so, if throughout the system we use Null Entities instead of nulls, then we will not get null reference exceptions. Let�s consider an example.
Let�s pretend that we build some kind of a caching system. Here, we define the ICacheStorage interface with three members: Remove, Store, and Retrieve. One of the useful implementations would be the HttpContextCacheStorage which caches data in the HttpContext. And here is the NullObjectCache which implements the same caching interface. What this implementation does? It literally does nothing.
Look at the possible client�s code. What might have happened if the argument of the PerformWork method would have been null? NullReferenceException would have been thrown from the attempt to retrieve an object. Now consider what will happen if we pass an instance of the NullObjectCache class. Nothing. Literally nothing. Sometimes this is a really desired behavior and in such cases, this pattern provides a safety net. So, remember, this pattern is useful when you know that there is a meaningful default behavior. �doing nothing� is a meaningful behavior in certain cases. If this is inappropriate for some reasons, then it might be better to stick with nulls or implement other strategies of avoiding nulls.
Null Object Pattern Learn the Null Object pattern in�C#.