java - How to avoid null checking - OOP -
i making game , have tile class contains item.
public clas tile{ item item; .... public void setitem(item item){ this.item = item; } }
when have reference tile want call interact()
method on item. how can without checking if object null
. don't think null
object pattern work in scenario because there mixed instance cohesion - subclass of item represent empty item have empty interact()
method.
you trying find way not check if object null, when null option.
in case, design check if item != null
, before execute item.interact()
, not anti-pattern or hacking solution.