假设我们有Book
一个包含year_published
公共字段的类。如果我想实现 NullObject 设计模式,我需要定义NullBook
行为相同Book
但不做任何事情的类。
NullBook
问题是,分配字段时的行为应该是什么?
Book book = find_book(id_value); //this method returns a NullBook instance because it cannot find the book
book.year_published = 2016; //What should we do here?!