Law of Demeter confusion in Java

Viewed 492

Am I breaking the “Law of Demeter”? For example i create a Class person which contains name, phone and id and it match the column in my database. When I want to fill my Order info using person's id.I do like this.

 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.That's break LoD. Somebody recommend that I can do like 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 in public method it still break the 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;
}}

Yeah Maybe it doesn't break LoD. But why?May be Client isn't coupled to the class Person.But it is coupled to Util. What are the advantages of LoD on this occasion.

1 Answers
Related