If I have 3 classes, let's for arguments sake call them:
- MainActivity
- GLRenderer
- OtherClass
Please Note that GLRenderer is not purely a proxy-class but does contain some proxy-methods.
The MainActivity initialises an int called value. Now, if OtherClass needs to change that value for whatever reason and it only has direct access to the GLRenderer Class (which in turn has access to the MainActivity class, what is the best way to get access to this variable?
Currently, I'm using a setter method in MainActivity, and then using another setter in GLRenderer (a proxy method really, as it simply passes the value through).
This works fine and is how I'm doing things at the moment. Here is some pseudo code (I realise this code may not compile, it's purely for the purpose of this question):
Main Activity
public class MainActivity extends Activity {
private int value;
public setValue(int valueToSet){
value = valueToSet;
}
}
Second Class
public class GLRenderer {
private MainActivity activity;
public GLRenderer(MainActivity activity){
this.activity = activity;
}
public setValue(int value){
activity.setValue(value);
}
}
Other Class
public class OtherClass {
private GLRenderer glRenderer;
public OtherClass(){
this.glRenderer = glRenderer;
}
public someMethod(){
glRenderer.setValue(5);
}
}
Is the above better than doing something like this: (Please see comments in OtherClass code)
GLRenderer
public class GLRenderer {
private MainActivity activity;
public GLRenderer(MainActivity activity){
this.activity = activity;
}
public MainActivity getActivity(){
return activity;
}
}
Other Class
public class OtherClass {
private GLRenderer glRenderer;
public OtherClass(){
this.glRenderer = glRenderer;
}
public someMethod(){
//I know this is considered bad.....
glrenderer.activity.setValue(5);
//But how about this - is this any different/better and if so, why?
glRenderer.getActivity().setValue(5);
}
}
I know the first method requires a shorter final instruction in OtherClass but using the above method, I could access anything from MainActivity without having to effectively duplicate my getter/setters (which can be a pain if I have a lot of things that I need to access).