java - Code Performance -
in both code single instance used i'm wondering approach better
for e.g.:
an interface
interface a{ void someoperation(); }
implementation of interface
class aimpl implements a{ private object a; public aimpl(object a){ this.a = a; } void someopearion(){ // ...... // } }
factory interface
interface factory{ geta(); }
factory implementation
class factoryimpl implement factory{ a; geta(){ if(a == null){ a=new aimpl(); } return a; } }
now there 2 approaches 1 basic difference use factory
approach 1:
class view{ view(){ somemethod(); } void somemethod(){ factory.geta().someoperation(); } }
approach 2:
class view{ a; view(){ this.a = factory.geta(); somemethod(); } void somemethod(){ a.someoperation(); } }
in approach 1 every operation need access view using factory while in approach 2 i'm using factory instance locally.
i think approach 2 better because not using method channing or interface callback.am right can 1 explain elaborate .
approach 2 can marginally faster if call somemethod()
several times, since save 1 method call. however, in real life not affect runtime of application, unless make millions of calls.
i chose between 2 approaches based on maintainability , code simplicity. if make same call in several places within 1 class, use approach2. otherwise approach 1 simpler , easier understand.
Comments
Post a Comment