How should I model associations between classes

I am modeling a domain model in my application. Obviously, some of the object relationships are associations, but these associations also have attributes. For instance:

Foo can have one-to-many bars. However, an association has attributes such as a date range, how long the association has been valid. So, the way I did this is as follows:

public interface Association<S, T> {
     public S getSource();

     public T getTarget();
}

Then for something like the above:

public class FooToBarAssociation implements Association<Foo, Bar> {
    public Foo getSource();
    public Bar getTarget();
    public Date getFromDate();
    public Date getToDate();
}

Then the class Foo has:

private List<FooToBarAssociation> associations;

My questions:

  • Is this a suitable way to model attribute relationships?

  • - / Foo? FooToBarAssociation -, , FooService, setAssociations, . , .

+3
2

IMHO , UML. , , , - Foo Assoc, , , Bar. , Assoc Assoc, Foo. Foo Assoc Bar Assoc. , . . , , , .

0

:

  • , .
  • - Foo.

, . , .

public class Foo {

    private Date startDate;
    private Date endDate;
    private Bar bar;

    public Date getStartDate() {
        return startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public Bar getBar() {
        return bar;
    }


}

, Foo. , Foo to Bar , . Foo Bar , .

0

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


All Articles