Link to the "parent" object

Suppose I have two classes that look like this:

public class Human {
     private String name;
     private String id;
     private List<Bike> bikes;
}

public class Bike {
    private String model;
    private String id;
    private String type;
}

Now, is it worth putting String humanIDinto a classroom bike? I can do almost any operation with the current structure, but if, for example, I get a bike object and want to know its owner, I need to iterate over a collection of people. On the other hand, if I put the humanID inside the bike, I feel that it will be curious redundant information, because the agregation itself says who the owner is. So can someone explain which one is the right approach and why?

+4
source share
3 answers

It is entirely up to your use.

  • :
    , , , . . . . .
  • : , , . , , . , , .
  • : , ", ", , . .
+4

-, , ( ):

  • , .

?

: Human, Bike HumanBike, , ​​ , .

:

enter image description here

Java?

Human Set Bike, , :

public class Human {
     private String name;
     private String id;
     private Set<Bike> bikes;
}

, Bike Human, :

public class Bike {
    private String model;
    private String id;
    private String type;
    private Human owner;
}

, , .

?

!

, , oris Schellekens , .

+1

, , owner , . : class HumanHasBike{ Human human; List<Bike> bikes;}

0

Source: https://habr.com/ru/post/1679757/


All Articles