oop - NullObject Pattern: How to handle fields? -
suppose have book
class contains year_published
public field. if want implement nullobject design pattern, need define nullbook
class behaves same book
not anything.
question is, should behavior of nullbook
when it's fields being assigned?
book book = find_book(id_value); //this method returns nullbook instance because cannot find book book.year_published = 2016; //what should here?!
the first thing should make properties private.
class nullbook { private year_published; // or solution2 private year_published = null; public setyearpublished(year_published) { this.year_published = null; // or solution2 nothing! } }
you can define field private in parent class, children have implement setter acces field
class book { private year_published; public setyearpublished(year_published) { this.year_published = year_published; } } class nullbook extends book { public setyearpublished(year_published) { parent::setyearpublished(null); } }
why use getters , setters? https://stackoverflow.com/a/1568230/2377164
Comments
Post a Comment