Enterprise Architect(EA) 是一個功能比較強悍的建模工具,本篇文章僅使用其 UML 建模功能,其他更多功能,可以Google。
一、簡單梳理C#中類與類、類與接口、接口與接口的關系

一、繼承 (子類 : 父類、子接口 : 父接口) Is 子類 : 父類 abstract class Fruit{} class Apple : Fruit{} 子接口 : 父接口 interface IBase{} interface ISon : IBase{} 二、實現 (類 : 接口) realse class Banna : IBase{} class Cana : Fruit,IBase{} 三、組合 Has (類中有類,類中有接口,接口中有類,接口中有接口) class House { //構造器中包含 House(IBase base){} // 方法中包含 void Invoke(IBase base){} //屬性中包含 IBase BaseInter{get{return null;}} // 包含類 void Show(Banna b){} } interface ISampleInterface { // 接口中有接口 void Show(IBase base); // 接口中有類 void Invoke(House house); } 其實組合還可以分為 更細的,這個在OOA&OOD的書上講的很多,不過我覺得 對C#而言就了解 上面的三種關系足矣
二、下載、安裝 EA及部分設置
我在網上找到漢化新世紀漢化的"Enterprise Architect 8.0.858",及相應注冊碼打包到百度網盤(http://pan.baidu.com/s/1pJ9JKaZ)
安裝過程略
將默認代碼模式設置為 C#,"工具 ==> 選項 ==> 代碼工程" 代碼工程默認語言改為 C#
三、需要關注的幾個ToolBox項目
Class 類
interface 接口
標號1Generalize(泛化)就上面講“繼承”關系
標號2 Compose(組成) 就是上面講的“組成”關系
標號3 Realize(實現)就是上面將的“實現”關系
四、畫類圖
以王翔設計模式書LSP和DIP原則一節的例子為例
LSP(里氏替換原則):子類可替換父類(實現可替換接口)
DIP(依賴倒置原則):高層對象與低層對象互補依賴,它們都依賴於抽象(依賴於抽象而非具體)
類圖:
實現代碼:

1 #region[抽象] 2 public interface IVehicle 3 { 4 void Run(); 5 } 6 #endregion 7 8 #region[低層對象] 9 public class Bicycle : IVehicle 10 { 11 12 public void Run() 13 { 14 // 自行車的實現 15 } 16 } 17 18 public class Train : IVehicle 19 { 20 public void Run() 21 { 22 // 火車的實現 23 } 24 } 25 #endregion 26 27 #region[高層對象] 28 public class Client 29 { 30 public void ShowAVehicleRun(IVehicle vehicle) 31 { 32 vehicle.Run(); 33 } 34 } 35 #endregion
具體操作