In the code example that i have below, i am accessing each property of the class through getters in main(), however I do not think this is a good design because the interfaceA
which is returned will have no idea about these getters even though programmatically it is right.
And to have a common function that returns both, each of them return different type. Is it a good idea to have the getters as a function in the interface itself? in that case i would need to implement in all the classes that i implement.
Please help me with this design on how to access properties of classes that are different from each other implementing the same interface.
public interface interfaceA {
method();
}
public classA implements interfaceA {
int a;
int b;
public method() { }
public getA() {
return a;
}
public getB() {
return b;
}
}
public classB implements interfaceA {
array arr;
public method() { }
public getArray() {
return arr;
}
}
public commonClass {
public getClass() {
if (condition) return new classA();
else
return new classB();
}
}
public void main() {
classDefinition = (new commonClass()).getClass();
if(condition)
var a = classDefinition.getA();
var b = classDefinition.getB();
else
var array = classDefinition.getArray();
}
Aucun commentaire:
Enregistrer un commentaire