2012-06-03 27 views
2

更新3代碼實現爲班圖

什麼跡象(單線,鑽石與明星和箭頭)在圖形下方的意思是(從Eric的DDD書P195):

enter image description here

任何代碼示例來說明,將不勝感激。

+0

順便說一句,你是怎麼用它來生成這個UML圖?我見過其他用戶使用yuml.me,這非常酷。 – Brady

+0

yuml.me不是免費的,如果我是對的!我相信那裏有免費的。但這是我會考慮的一個選項。感謝您的建議。 – Pingpong

+0

你在這篇文章中使用了哪些工具? – Brady

回答

3

金剛石是組合物(也稱爲聚集),或一個has-a關係。箭頭是繼承關係,或者是is-a關係。該行是一個協會。這導致了一個問題:組合和協會有什麼區別。答案是組成更強,通常擁有另一個對象。如果主對象被銷燬,它也將銷燬其組成對象,但不會銷燬它的關聯對象。

在你的榜樣,設施包含(具有-A)LoanInvestment和LoanInvestment繼承的(是-A)投資

這裏是class diagrams using UML一個很好的說明。

這裏是C++中的代碼示例,我不知道C#的不夠好,和Id可能搞砸了:)

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 
}; 
+0

設施和貸款之間的單線是什麼?貸款是否包含貸款投資的收集? – Pingpong

+0

@Pingpong額外的兩行不是標準的UML。標準的UML有一個黑色或白色的鑽石,用於(has-a或由...組成)和用於is-a的完整箭頭。空箭頭是has-a上的定向導航。 –

+0

@Pingpong,我更新了單行的答案。我可以給你一個很好的C++代碼示例,會有幫助嗎? – Brady