I am currently learning about
delegate and the Law of Demeter. I understand the Law of Demeter, but I can't seem to find a good example of where using
delegate to avoid breaking the law would actually even be useful.
I want to find an example related to my project, since I have to give a presentation. The only line of code that I found that could possibly be breaking the Law of Demeter is the following:
Promotions and it is reaching across another model to execute a
find call based on promotions attribute
promo_type. From my understanding this is a violation of the Law of Demeter, and I should implement the following solution.
class Game < ActiveRecord::Base has_many :promotions delegate :find_by_promo_type, :to => :promotion end
Can you give me an example where this would actually be useful besides "the Law of Demeter says so".
The only thing I can think of, is for some reason I wanted to change the name of
promos, then the solution would be useful because I would only need to make the following change and
:find_by_promo_type will still work for
class Game < ActiveRecord::Base has_many :promos delegate :find_by_promo_type, :to => :promos end
The only thing is, I believe this argument is flawed. If I were to change a models name, I would also have to refactor code in many other places that don't even break the Law of Demeter. It's hard to believe that this is all the Law of Demeter can accomplish in regards to this example.
Can somebody please help me understand this.