Consider an example of a simple 'MyCalendar' class with 3 getters 'getDay, getMonth, and getYear'. If I pass 'MyCalendar' object to my another class which of the following options would be a good 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 the values obtained from injected object's getter as 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 the answer is choice 1 then : if a field needs to be accessed mutiple times like in a loop: for (..some imaginary usecase) { mycal.getDate(); } In this case does it benefit to have a local copy ?
Use option 1. It is the sound option oriented approach. You can just delegate tasks to the calendar object that you already have created. The only reason I could see to make a copy is if the original data is mutable and you do not want the field in foo to change, even then I would preserve it in object form, but make a copy.