2

更新 3

下面的图表(来自 Eric 的 ddd 书 p195)中的符号(单线、带星形的菱形和箭头)是什么意思:

在此处输入图像描述

任何用于说明的代码示例将不胜感激。

4

1 回答 1

3

钻石是组合(也称为聚合)或has-a关系。箭头是继承或is-a关系。该行是一个协会。这就引出了一个问题:组合和关联之间有什么区别。答案是组合更强大,通常拥有另一个对象。如果主对象被销毁,它也会销毁它的组合对象,但不会销毁它的关联对象。

在您的示例中,Facility 包含 (has-a) LoanInvestment 和 LoanInvestment 从 (is-a) Investment 继承

这是使用 UML的类图的极好描述。

这是 c++ 中的代码示例,我不太了解 c#,我可能把它搞砸了 :)

class Facility
{
public:
    Facility() : loan_(NULL) {}

    // Association, weaker than Composition, wont be destroyed with this class
    void setLoan(Loan *loan) { loan_ = loan; } 

private:
    // Composition, owned by this class and will be destroyed with this class
    // Defined like this, its a 1 to 1 relationship
    LoanInvestment loanInvestment_;
    // OR
    // One of the following 2 definitions for a multiplicity relation
    // The list is simpler, whereas the map would allow faster searches
    //std::list<LoanInvestment> loanInvList_;
    //std::map<LoanInvestment> loanInvMap_;

    Loan *loan_:
    // define attributes here: limit
};

class Loan
{
public:
    // define attributes here: amount
    // define methods here: increase(), decrease()
private:
    // 1 to 1 relationship, could consider multiplicity with a list or map
    LoanInvestment loanInvestment_;
};

class Investment
{
    // define attributes here: investor, percentage
};

class LoanInvestment : public LoanInvestment
{
    // define attributes here
};
于 2012-06-03T11:58:22.827 回答