双向关联

时间:2016-10-13 17:41:16

标签: java uml polymorphic-associations bidirectional

关联的概念以及它与UML设计的关系。如果有专家能给我一个想法或代码设计与UML图片有关,我将不胜感激。

There are 3 differently designed UML as seen below

先谢谢!!!

public class Borrower {
    private String name;

    public Borrower() {
        Equipment[] tester = new Equipment[5];
        tester[0] = new Equipment(this);
    }
}
public class Equipment {
    Borrower[] person = new Borrower[10];

    public Equipment(Borrower b) {
        person[0] = b;
    }
}

2 个答案:

答案 0 :(得分:0)

一种可能的解决方案是

public class Loan {
    private final Date date;
    private final Borrower borrower;
    private final Equipment equipment;

    Loan(Borrower borrower, Equipment equipment, Date date) {
        this.borrower = borrower;
        this.equipment = equipment;
        this.date = date;
    }
}

public class Borrower {
    private String name;
    private final List<Loan> loans = new ArrayList<>();

    public Borrower(String name, Equipment equipment, Date date) {
        this.name = name;
        borrow(equipment, date);
    }

    public void borrow(Equipment equipment, Date date) {
        Loan loan = new Loan(this, equipment, date);
        loans.add(loan);
        equipment.addLoan(loan);
    }
}

public class Equipment {
    private final List<Loan> loans = new ArrayList<>();

    void addLoan(Loan loan) {
        loans.add(loan);
    }
}

答案 1 :(得分:0)

第二和第三个表示是显示相同事物的替代方法。它被称为关联类。这里Loan是关联类,它充当它连接的两个类之间的链接,从而添加一些额外的属性/操作。关联类始终表示1-m-n-1关系。

第一个图表示例是面向业务的表示。它使用带有三角形的公共位置标签,显示读取顺序。它将如何实现这一点。关联类是执行此操作的常用方法。但你也可以在Equipment内保留Borrower数组。您可能会注意到这里没有借款日期。