Since your commonValue
can change, a cleaner way (IMHO) would be:
public class MyFactory {
private final int commonValue;
public MyFactory (int commonValue) { this.commonValue = commonValue; }
public MyInterface myObject(int value) {
return new MyObject(value, commonValue);
}
public MyInterface yourObject(String str) {
return new YourObject(str, commonValue);
}
}
And when you need to reset it, just create a new factory.
The benefits are:
- it is clear what the value is (if you used a mutable value with a setter, some code could affect non related code)
- it is thread safe