-
-
Save jbrains/1259077 to your computer and use it in GitHub Desktop.
Separating use from construction - Before
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 { | |
/** @deprecated */ | |
public void actionMethod() { | |
actionMethod(Service.getInstance()); | |
} | |
// A SEAM! A SEAM! New clients should use me!! | |
public void actionMethod(ServiceDelegate serviceDelegate) { | |
// Other things | |
serviceDelegate.doService(); | |
// Other things | |
} | |
// Am I a Service? Am I a Delegate? Can you tell? Do you care? | |
abstract class Service : ServiceDelegate { | |
/** @deprecated Haven't all my clients died yet!? */ | |
public static Service getInstance() { | |
return new ProductionService(new ProductionServiceDelegate()); | |
} | |
public abstract void doService(); | |
} | |
/** @deprecated Depend on ServiceDelegate directly, if you can */ | |
class ProductionService : Service { | |
private final ProductionServiceDelegate delegate; | |
public ProductionService(ProductionServiceDelegate delegate) { | |
this.delegate = delegate; | |
} | |
public void doService() { | |
delegate.doService(); | |
} | |
} | |
// Look how mockable I am! New clients should use me!! | |
interface ServiceDelegate { | |
void doService(); | |
} | |
class ProductionServiceDelegate() { | |
public ProductionServiceDelegate() { | |
// copy from old ProductionService | |
} | |
public void doService() { | |
// copy from old ProductionService | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome example. Thanks.