In both code single instance is used but I'm just wondering which approach is better
For e.g.:
An Interface
interface A{
void someOperation();
}
Implementation of that interface
class AImpl implements A{
private Object A;
public AImpl(Object A){
this.A = A;
}
void someOpearion(){
//
......
//
}
}
Factory interface
interface Factory{
A getA();
}
Factory Implementation
class FactoryImpl implement Factory{
A a;
A getA(){
if(a == null){
a=new AImpl();
}
return a;
}
}
Now There are two approaches with just 1 basic difference to use this Factory
Approach 1:
class View{
View(){
someMethod();
}
void someMethod(){
factory.getA().someOperation();
}
}
Approach 2:
class View{
A a;
View(){
this.a = factory.getA();
someMethod();
}
void someMethod(){
a.someOperation();
}
}
In approach 1 for every operation I need to access view using factory while in approach 2 I'm using factory instance locally.
I think approach 2 is better because its not using method Channing or interface callback.Am I right Can some one explain elaborate this .