Sunteți pe pagina 1din 19

The Null Object Pattern

Fly

www.freepik.com
abstract class Duck
{
public FlyBehaviour fly { get; set; }
public SwimBehaviour swim { get; set; }
public EatBehaviour eat { get; set; }

public void PerformFly()


{
fly.Fly();
}

public void PerformSwim()


{
swim.Swim();
}

public void PerformEat()


{

What if any behaviour is null?


}
}
eat.Eat();
public void PerformFly()
{
if (fly != null)
fly.Fly();
}

public void PerformSwim()


{
if (swim != null)
swim.Swim();
}

public void PerformEat()


{
if (eat != null)
eat.Eat();
}
Definition

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.
• This is a behavioural pattern as it defines a manner for controlling
communication between classes or entities.
• When you develop classes that have dependencies, you will
sometimes have cases where a dependency is not needed, perhaps
because the normal behaviour of the dependency is not required.
• One way to deal with such a situation is to use a null reference
instead of a real object for that dependency.
• Unfortunately, using null tends to complicate the code.
• Wherever you need to use a member from the object, you must first
check if it is null, as calling a method or accessing a property of a null
reference will cause an exception.
The Null Object pattern provides a
solution to this problem.
• Instead of passing null references and checking for the presence of
null before any operation, you create a specific class that represents a
non-functional dependency.
• This class
• implements an expected interface or inherits from an abstract class but
includes no functionality.
• its methods and properties perform no action and return default, fixed
values.
• This allows any dependant object to use null object dependencies
without performing any pre-checks, simplifying the
code.
• The null object pattern is usually used with other design patterns:
• Singleton. This limits the number of instances to one, which is ideal as
null objects generally have no state and no functionality so creating additional
objects would add unnecessary overhead to your software. (This the best
way to implement the null object to conserve resources.)
• Strategy pattern. When one of the strategies requires no
functionality, a null object can be used.
• Factory method pattern. Where the factory may return a null
object instance.
• Template method pattern. If there are cases when
sometimes template is called and sometimes not then, in order to avoid the
checking a Null Object can be use to implement a Concrete Template that
does nothing.
Intent
• Provide an object as a surrogate for the lack of an object of a given
type.
• The Null Object Pattern provides intelligent do nothing behavior,
hiding the details from its collaborators.
Removing old functionality

The Null Object can be used to remove old


functionality by replacing it with null objects. The
big advantage is that the existing code doesn't
need to be touched.
Implementing the Null Object Pattern

Context
• This class has a dependency that may or may not be required.
• Where no functionality is required in the dependency, it will execute the methods of a null object.
Implementing the Null Object Pattern

DependencyBase
• This abstract class is the base class for the various available dependencies that the Client may use.
• This is also the base class for the null object class.
• Where the base class provides no shared functionality, it may be replaced with an interface.
Implementing the Null Object Pattern

Dependency
• This class is a functional dependency that may be used by the Client.
Implementing the Null Object Pattern

NullObject.
• 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.
Implementing the Null Object Pattern
public abstract class DependencyBase
{
public abstract void Operation();
}

public class Dependency : DependencyBase


{
public override void Operation()
{
Console.WriteLine("Dependency.Operation() executed");
}
}

public class NullObject : DependencyBase


{
public override void Operation() { }
}
Implementing the Null Object Pattern
class Program
{
public class Client static void Main(string[] args)
{ {
DependencyBase _dependency; List<Client> Clients = new List<Client>();

public Client(DependencyBase dependency) Clients.Add(new Client(new Dependency()));


{ Clients.Add(new Client(new NullObject()));
_dependency = dependency; Clients.Add(new Client(new Dependency()));
}
foreach (Client Current in Clients)
public void DoSomething() Current.DoSomething();
{
_dependency.Operation(); Console.ReadLine();
} }
} }
Implementing the State Pattern
The End

S-ar putea să vă placă și