javalaw-of-demeter

Is this a violation of the Law of Demeter? vs. readable code


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.


Solution

  • The code below brakes [sic] apparently the Law of Demeter, i.e. methods getServer().methodx(...). From other side it looks pretty compact = better readable?

    The point of your design is lost on me. If compactness is your goal, then would not this be even better?

    class Manager {
        private Server server;
    
        public Manager(Server server) {
            this.server = server;
        }
    
        ResultSet find(String searchText) {
            server.find(searchText);
        }
    
        ResultSet fetch(String fetchText) {
            server.fetch(fetchText);
        }
    
        void save(String saveText) {
            server.save(saveText);
        }
    }
    

    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.

    If the law is violated, them how to refactor this code?

    I'm still liking what I presented above.

    Is the following solution acceptable (does it not look like a code duplication)?: [...]

    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.