java - Dependency object be saved as instance variable or just the values obtained from getters? -
consider example of simple 'mycalendar' class 3 getters 'getday, getmonth, , getyear'. if pass 'mycalendar' object class of following options approach.
option 1: call required parameters through injected object's getters when needed.
class foo { mycalendar mycal; class foo(mycalendar mycal) { this.mycal = mycal } }
or
option 2: assign values obtained injected object's getter part of initialization.
class foo { day d; month m; year y; class foo(mycalendar mycal) { d = mycal.getday(); m = mycal.getmonth(); y = mycal.getyear(); } }
if answer choice 1 : if field needs accessed mutiple times in loop: (..some imaginary usecase) { mycal.getdate(); } in case benefit have local copy ?
use option 1. sound option oriented approach. can delegate tasks calendar object have created. reason see make copy if original data mutable , not want field in foo change, preserve in object form, make copy.
Comments
Post a Comment