The code below brakes apparently the Law of Demeter, i.e. methods getServer().methodx(...). From other side it looks pretty compact = better readable?
abstract class BaseManager {
ResultSet find(String searchText) {
return getServer().find(searchText);
}
ResultSet fetch(String fetchText) {
return getServer().fetch(fetchText);
}
void save(String saveText) {
getServer().save(saveText);
}
abstract BaseManager getServer();
}
class Server1Manager extends BaseManager {
@Override
protected BaseManager getServer() {
return server1;
}
}
class Server2Manager extends BaseManager {
@Override
protected BaseManager getServer() {
return server2;
}
}
If the law is violated, them how to refactor this code? Many thanks in advance.
The point of your design is lost on me. If compactness is your goal, then would not this be even better?
In addition to being even more compact and clear, that happens to conform to the Law of Demeter. Moreover, it follows the principle of preferring composition over inheritance, which I think you'll see is favored (but not implied) by the Law of Demeter.
I'm still liking what I presented above.
If you presented that to me for code review, I would certainly ask what you think you gain from inheritance there. One might argue a bit over whether your code is technically duplicative, but it is certainly longer and more complex than the non-inheritance version I presented. And the non-inheritance version leaves no question about code duplication.