Null对象的设计
/** * * @author Rock Lee * @Description 标记接口 */ public interface Null { }
/** * * @author Rock Lee * @Description */ public class Person { private String first; private String last; private String address; public static final Person NULL = new NullPerson(); public Person() { super(); } public Person(String first, String last, String address) { super(); this.first = first; this.last = last; this.address = address; } /** * * @author Rock Lee * @Description 内部类 对标记接口实现 */ public static class NullPerson extends Person implements Null { private NullPerson() { super("None", "None", "None"); } @Override public String toString() { return "NullPerson"; } } @Override public String toString() { return "Person [first=" + first + ", last=" + last + ", address=" + address + "]"; } }
public class TestNullObject { @Test public void test(){ Person p = Person.NULL; System.out.println(p); } }