Am I breaking the Law of Demeter? For example, I create a person of a class that contains a name, phone and ID, and it corresponds to a column in my database. When I want to fill out my order information using a user ID. I like it.
public static void fill(Order order) { DatabaseComponent databaseComponent = new DatabaseComponent(); Person person = databaseComponent.getById(order.getUserId()); order.setName(person.getName()); order.setPhone(person.getPhone()); }
I use getName and getPhone return by databaseComponent. This breaks the LoD. Someone would recommend that I do this
public void fill(Order order) { DatabaseComponent databaseComponent = new DatabaseComponent(); Person person = databaseComponent.getById(order.getId()); fillOrder(order,person); } private void fillOrder(Order order,Person person){ order.setPhone(person.getPhone()); order.setName(person.getName()); return; }
But I think that in the public method, it still interrupts LoD. Some people use this method.
public class Util { public static void fillOrder(Order order,Person person){ order.setPhone(person.getPhone()); order.setName(person.getName()); return; }}
Yes, maybe this will not break LoD. But why? Maybe the Client is not associated with the Person.But class, it is associated with Util. What are the benefits of LoD in this case.
source share