Design Pattern -- 22. Behavioral. Null Object. Pattern (Java Sample)

Source: Internet
Author: User
Intent

The intent of a Null Object is to encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior. In short, a design where "nothing will come of nothing"

Use the Null Object pattern when

  • An object requires a collaborator. The Null Object pattern does not introduce this collaboration-it makes use of a collaboration that already exists
  • Some collaborator instances shoshould do nothing
  • You want to abstract the handling of null away from the client
Problem

Given that an object reference may be optionally null, and that the result of a null check is to do nothing or use some default value, how can the absence of an object-the presence of a null reference-be treated transparently?

Discussion

Sometimes a class that requires a collaborator does not need the collaborator to do anything. However, the class wishes to treat a collaborator that does nothing the same way it treats one that actually provides behavior.

Consider for example a simple screen saver which displays bils that move about the screen and have special color effects. this is easily achieved by creating a Ball class to represent the bils and using a Strategy pattern to control the ball's motion and another Strategy pattern to control the ball's color.

It wowould then be trivial to write strategies for each different types of motion and color effects and create bils with any combination of those. however, to start with you want to create the simplest strategies possible to make sure everything is working. and these strategies cocould also be useful later since you want as strategies as possible strategies.

Now, the simplest strategy wocould be no strategy. that is do nothing, don't move and don't change color. however, the Strategy pattern requires the ball to have objects which implement the strategy interfaces. this is where the Null Object pattern becomes useful.

Simply implement a NullMovementStrategy which doesn't move the ball and a NullColorStrategy which doesn't change the ball's color. both of these can probably be implemented with essential no code. all the methods in these classes do "nothing ". they are perfect examples of the Null Object Pattern.

The key to the Null Object pattern is an abstract class that defines the interface for all objects of this type. the Null Object is implemented as a subclass of this abstract class. because it conforms to the abstract class 'interface, it can be used any place this type of object is needed. as compared to using a special "null" value which doesn't actually implement the abstract interface and which must constantly be checked for with special code in any object which uses the abstract interface.

It is sometimes thought that Null Objects are over simple and "stupid" but in truth a Null Object always knows exactly what needs to be done without interacting with any other objects. so in truth it is very "smart."

Structure

  • Client-

    • Requires a collaborator.
  • AbstractObject-
    • Declares the interface for Client's collaborator
    • Implements default behavior for the interface common to all classes, as appropriate
  • <strong>RealObject</strong>-
    • Defines a concrete subclass of specified actobject whose instances provide useful behavior that Client expects
  • <strong>NullObject</strong>-
    • Provides an interface identical to initialize actobject's so that a null object can be substituted for a real object
    • Implements its interface to do nothing. What exactly it means to do nothing depends on what sort of behavior Client is expecting
    • When there is more than one way to do nothing, more than one NullObject class may be required
Rules of thumb
  • The Null Object class is often implemented as a Singleton. since a null object usually does not have any state, its state can't change, so multiple instances are identical. rather than use multiple identical instances, the system can just use a single instance repeatedly.
  • If some clients perform CT the null object to do nothing one way and some another, multiple NullObject classes will be required. If the do nothing behavior must be customized at run time,NullObjectClass will require pluggable variables so that the client can specify how the null object shocould do nothing (see the discussion of pluggable adaptors in the Adapter pattern). This may generally be a symptom ofAbstractObjectNot having a well defined (semantic) interface.
  • A Null Object does not transform to become a Real Object. if the object may decide to stop providing do nothing behavior and start providing real behavior, it is not a null object. it may be a real object with a do nothing mode, such as a controller which can switch in and out of read-only mode. if it is a single object which must mutate from a do nothing object to a real one, it shocould be implemented with the State pattern or perhaps the Proxy pattern. in this case a Null State may be used or the proxy may hold a Null Object.
  • The use of a null object can be similar to that of a Proxy, but the two patterns have different purposes. A proxy provides a level of indirection when accessing a real subject, thus controlling access to the subject. A null collaborator does not hide a real object and control access to it, it replaces the real object. A proxy may eventually mutate to start acting like a real subject. A null object will not mutate to start providing real behavior, it will always provide do nothing behavior.
  • A Null Object can be a special case of the Strategy pattern. Strategy specifies several ConcreteStrategy classes as different approaches for accomplishing a task. If one of those approaches is to consistently do nothing, thatConcreteStrategyIsNullObject. For example, a Controller is a View's Strategy for handling input, and NoController is the Strategy that ignores all input.
  • A Null Object can be a special case of the State pattern. normally, each ConcreteState has some do nothing methods because they're not appropriate for that state. in fact, a given method is often implemented to do something useful in most states but to do nothing in at least one state. if a particle ConcreteState implements most of its methods to do nothing or at least give null results, it becomes a do nothing state and as such is a null state.
  • A Null Object can be used to allow a Visitor to safely visit a hierarchy and handle the null situation.
  • Null Object is a concrete collaborator class that acts as the collaborator for a client which needs one. the null behavior is not designed to be mixed into an object that needs some do nothing behavior. it is designed for a class which delegates to a collaborator all of the behavior that may or may not be do nothing behavior.
Null Object In Java

Simpliest example ever

  1: class NullOutputStream extends OutputStream {
  2: public void write(int b) {
  3: // Do nothing
  4: }
  5: }
  6: 
  7: class NullPrintStream extends PrintStream {
  8: public NullPrintStream() {
  9: super(new NullOutputStream());
 10: }
 11: }
 12: 
 13: class Application {
 14: private PrintStream debugout;
 15: public Application(PrintStream debugout) {
 16: this.debugout = debugout;
 17: }
 18: 
 19: public void go() {
 20: int sum = 0;
 21: for (int i = 0; i < 10; i++) {
 22: sum += i;
 23: debugout.println("i = " + i);
 24: }
 25: System.out.println("sum = " + sum);
 26: }
 27: }
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.