【问题标题】:Code Implementation for Class Diagram in UMLUML中类图的代码实现
【发布时间】:2012-06-03 11:45:23
【问题描述】:

更新 3

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

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

【问题讨论】:

标签: c# architecture domain-driven-design uml


【解决方案1】:

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

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

这是对class diagrams using 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
};

【讨论】:

  • 融资和贷款之间的单线呢?贷款是否包含 LoanInvestment 的集合?
  • @Pingpong 额外的两行不是标准的UML。标准 UML 有一个黑色或白色菱形表示(has-a 或 contains-of),完整箭头表示 is-a。空箭头是 has-a 上的定向导航。
  • @Pingpong,我更新了单行的答案。我可以给你一个很好的 C++ 代码示例,会有帮助吗?
  • @Brady Inheritance 看起来不像。
  • @DannyVarod,您指的是哪几行?通常,聚合线表示每个聚合线有多少,例如“1 0..*”或“1 1..*”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
相关资源
最近更新 更多