in object-oriented computer programming, a null object is an object with no referenced value or with defined neutral ("null") behavior. the null object design pattern describes the uses of such objects and their behavior (or lack thereof).
public class dependency implements dependencybase, nullable {
@override
public void operation() {
system.out.print("test!");
}
@override
public boolean isnull() {
return false;
}
}
这是空对象,对原有对象的行为进行了空实现。
public class nullobject implements dependencybase{
@override
public void operation() {
// do nothing
}
@override
public boolean isnull() {
return true;
}
}
public class factory {
public static dependencybase get(nullable dependencybase){
if (dependencybase == null){
return new nullobject();
}
return new dependency();
}
}