I wish to design a class to have helper sort of methods on some objects. what is usually the best approach to do so. All the functions will act on these two objects.
SomeObj someObj;
AnotherObj anotherObj;
public SomeClass(SomeObj someObj,AnotherObj anotherObj){
this.someObj=someObj;
this.anotherObj=anotherObj;
}
somefunction(){
//act on the instance vars
}
or should I have simple class with no instance vars (stateless) and make function to accept the required variables
public SomeClass(){
}
somefunction(SomeObj someObj,AnotherObj anotherObj){
//act on the local vars
}
Which approach is considered good in terms of design.
If the only method that will act on the
SomeObjandAnotherObjobjects is someFunction() then they probably dont need to be stored as instance vars. If they will be used/needed to modify the state ofSomeClassfrom someFunction() and other internal methods, then I would say that they should indeed be stored.You could consider a few alternatives regarding the
SomeClassclass implementation: