1

I am making a game and I have a Tile class that contains an Item.

public clas Tile{
    Item item;
    ....
    public void setItem(Item item){
         this.item = item;
    }

}

When I have a reference to the Tile I want to call the interact() method on the item. How can I do this without checking if the object is null. I don't think the Null Object pattern will work in this scenario because there will be mixed instance cohesion - a subclass of item that would represent an empty item would have an empty interact() method.

7
  • 1
    I don't understand your reason for not using the null object pattern. A null object is supposed to be non-reactive, or at least reasonable, across its interface. In any case, unless you have a flag that indicates if the tile has an item (basically duplicating item null-ness) I don't see what the other options would be. I guess I don't understand what's wrong with a NoItem item. Commented Apr 10, 2015 at 2:16
  • Because it's mixed instance cohesion Commented Apr 10, 2015 at 2:18
  • 3
    ... Then check for null. I think you're being overly pedantic, since this would be true for any null object (which I view as a special case anyway). A null object by definition won't implement most functionality of a non-null implementation. Commented Apr 10, 2015 at 2:22
  • I think I may be overly pedantic... Commented Apr 10, 2015 at 2:24
  • every object will have multiple sub-objects . now every method might not need all the objects . -----------if tile is a board game , then u dont need to bother of attributes like color/colour . at every function which can crash on nulls , just check for required member fields and not title as a whole . Commented Apr 10, 2015 at 2:34

2 Answers 2

1

You are trying to find a way to not check if the object is null, when null is an option.

In this case, design to check if item != null, before execute item.interact(), is not an anti-pattern or hacking solution.

Sign up to request clarification or add additional context in comments.

Comments

0

The stereotypical solution to the mixed instance cohesion problem is to create subclasses (and make sure that the superclass doesn't expose the subclass-specific functionality).

So you create a ItemTile and NonItemTile subclass of Tile.

public abstract class Tile {
    public abstract void interact();
}

public class ItemTile extends Tile {
    private final Item item;
    ....
    public ItemTile(Item item) {
        // Null-check to enforce contract - could be omitted if you make this
        // the responsibility of the caller.
        if (item == null)
           throw new NullPointerException("item");
        this.item = item;
    }

    public void interact() {
        item.interact();
    }
}

2 Comments

I don't think this would work because an item can be picked up and that would break the contract for ItemTile
That sounds relevant. You should put that in the question so people can take that into account when answering the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.