How to obey Law of Demeter?

后端 未结 4 1600
终归单人心
终归单人心 2021-02-01 05:05

Any time I see articles about Law of Demeter the author never seems to give a solid example of how to obey this law. They all explain what it is and show an example of breaking

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 05:39

    I'd have a Band answer the question who's playing this instrument?

    public String whoIsPlaying(String instrument);
    

    Then if you need a guitarist's name you'd say:

    band.whoIsPlaying("guitar");
    

    Nothing stops you from pinging other instruments as well.

提交回复
热议问题