How to solve the violations of the Law of Demeter?

后端 未结 10 1247
一个人的身影
一个人的身影 2021-01-29 22:23

A colleague and I designed a system for our customer, and in our opinion we created a nice clean design. But I\'m having problems with some coupling we\'ve introduced. I could t

10条回答
  •  礼貌的吻别
    2021-01-29 23:07

    I know I'm going to get downvoted to total annihilation but I must say I sort of dislike Law of Demeter. Certainly, things like

    dictionary["somekey"].headers[1].references[2]
    

    are really ugly, but consider this:

    Kitchen.Ceiling.Coulour
    

    I have nothing against this. Writing a bunch of functions just to satisfy the Law of Demeter like this

    Kitchen.GetCeilingColour()
    

    just looks like a total waste of time for me and actually gets is my way to get things done. For example, what if the requirements change and I will need the ceiling height too? With the Law of Demeter, I will have to write an other function in Kitchen so I can get the Ceiling height directly, and in the end I will have a bunch of tiny getter functions everywhere which is something I would consider quite some mess.

    EDIT: Let me rephrase my point:

    Is this level of abstracting things so important that I shall spend time on writing 3-4-5 levels of getters/setters? Does it really make maintenance easier? Does the end user gain anything? Is it worth my time? I don't think so.

提交回复
热议问题