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

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -