Created
September 25, 2011 21:31
-
-
Save philipschwarz/1241180 to your computer and use it in GitHub Desktop.
Separating use from construction - After
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BusinessObject { | |
public void actionMethod() { | |
// Other things | |
//No Change! | |
Servicec myServiceObject = Service.getInstance(); | |
myServiceObject.doService(); | |
// Other things | |
} | |
} | |
abstract class Service { | |
private Service(){ //any needed construction behavior } | |
public static Service getInstance() { | |
return ServiceFactory.getService() | |
} | |
abstract void doService(); | |
} | |
class ServiceFactory{ | |
public static Service getService(){ | |
if (someCondition) { | |
return new Service_Impl1(); | |
} else { | |
return new Service_Impl2() | |
} | |
} | |
} | |
Service_Impl1 : Service { | |
void doService() { // one implementation } | |
} | |
Service_Impl2 : Service | |
void doService() { // different implementation | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't see how
ServiceFactory
helps here. It seems to muddy the waters. I would inlineServiceFactory.getService()
and put the conditional logic directly intoService.getInstance()
with a REFACTOR comment that says "push this decision out to clients!"Making
Service
abstract certainly sounds reasonable, but a REFACTOR comment that says "make me an interface, please".