UML——Unified modeling language UML (統一建模語言),是一種用於軟件系統 分析和設計的語言工具,它用於幫助軟 件開發人員進行思考和記錄思路的結果 。UML本身是一套符號的規定,就像數學 符和化學符號一樣,這些符號用於描 述軟件模型中的各個元素和他們之間的 關系,比如類、接口、實現、泛化、依 賴、組合、聚合等
前言
UML圖分類:
-
用例圖(use case)
-
靜態結構圖:類圖、對象圖、包圖、組件圖、部署圖
-
動態行為圖:交互圖(時序圖與協作圖)、狀態圖、活動圖
-
類圖 - 依賴關系(Dependence)
類圖 - 依賴關系(Dependence)
簡介
只要是在類中用到了對方,那么他們之間就存在依賴關系。如果沒有對方,連編 繹都通過不了。
應用案例
Java代碼
//被依賴者
public class PersonDao{}
public class IDCard{}
public class Person{}
public class Department{}
//依賴者
public class PersonServiceBean {
//成員屬性的方式
private PersonDao personDao;
//參數類型的方式
public void save(Person person) {
}
//返回類型的方式
public IDCard getIDCard(Integer personid) {
return null;
}
//局部變量的方式
public void modify() {
Department department = new Department();
}
}
UML類圖 + PlantUML代碼
@startuml
class PersonDao
class IDCard
class Person
class Department
class PersonServiceBean{
- PersonDao personDao
+ void save(Person person)
+ IDCard getIDCard(Integer personid)
+ void modify()
}
PersonDao <.. PersonServiceBean //PersonServiceBean依賴PersonDao
IDCard <.. PersonServiceBean
Person <.. PersonServiceBean
Department <.. PersonServiceBean
@enduml
小結(產生依賴關系的幾種方式)
- 類中用到了對方
- 如果是類的成員屬性
- 如果是方法的返回類型
- 是方法接收的參數類型
- 方法中使用到(局部變量)
類圖 - 泛化關系(generalization)
簡介
泛化關系實際上就是繼承關系,他是依賴關系的特例
應用案例
Java代碼
//抽象類
public abstract class DaoSupport {
public void save(Object entity) {
}
public void delete(Object id) {
}
}
/**
* 泛化關系
*/
public class PersonServiceBean extends DaoSupport{}
UML類圖 + PlantUML代碼
@startuml
/'聲明/定義'/
/'抽象類'/
abstract class DaoSupport{
+ void save(Object entity)
+ void delete(Object id)
}
class PersonServiceBean
/'表達關系'/
DaoSupport <|-- PersonServiceBean
@enduml
小結
- 泛化關系實際上就是繼承關系
- 如果A類繼承了B類,我們就說A和B存在泛化關系
類圖—實現關系(Implementation)
簡介
實現關系實際上就是A類實現B接口,他是依賴關系的特例
應用案例
Java代碼
public interface PersonService {
public void delete(Interger id);
}
//實現接口
public class PersonServiceBean implements PersonService {
@Override
public void delete(Integer id) {
}
}
UML類圖 + PlantUML代碼
@startuml
/'聲明/定義'/
interface PersonService
class PersonServiceBean
/'表示關系'/
PersonService <|... PersonServiceBean
@enduml
類圖—關聯關系(Association)
簡介
- 關聯關系實際上就是類與類之間的聯系,他是依賴關系的特例
- 關聯具有導航性:即雙向關系或單向關系
- 關系具有多重性:如“1”(表示有且僅有一個),“0…”(表示0個或者多個), “0,1”(表示0個或者一個),“n…m”(表示n到 m個都可以),“m…*”(表示至少m 個)。
應用案例
類圖—聚合關系(Aggregation)
簡介
聚合關系表示的是整體和部分的關系,整體與部分可以分開。聚合關系是關聯關系的特例,所以他具有關聯的導航性與多重性。
應用案例
如:一台電腦由鍵盤(keyboard)、顯示器(monitor),鼠標等組成;組成電腦的各個 配件是可以從電腦上分離出來的,使用帶空心菱形的實線來表示:
Java代碼
public class Monitor {
}
public class Mouse {
}
public class Computer {
/**鼠標可以和Computer分離*/
private Mouse mouse;
/**顯示器可以和Computer分離*/
private Monitor monitor;
public void setMouse(Mouse mouse) {
this.mouse = mouse;
}
public void setMonitor(Monitor monitor) {
this.monitor = monitor;
}
}
UML類圖 + PlantUML代碼
@startuml
/'聲明'/
class Monitor
class Mouse
class Computer{
- Monitor Monitor
- Mouse mouse
}
/'表示關系'/
Computer o-- Monitor /'Monitor聚合到Computer'/
Computer o-- Mouse
@enduml
小結
如果A類使用了B類但是不是必須使用到B類,並不是創建A時B類必須被創建/馬上被創建(並不是同生共死)。那么他們就是聚合關系
類圖—組合關系(Composition)
簡介
組合關系:也是整體與部分的關系,但是整體與部分不可以分開。
再看一個案例:在程序中我們定義實體:Person與IDCard、Head, 那么 Head 和 Person 就是 組合,IDCard 和 Person 就是聚合。
應用案例1
Java代碼
public class IDCard{}
public class Head{}
public class Person{
//IDCard並不是必須存在/實例化/創建的,是聚合關系
private IDCard card;
//Person 類創建的時候,Head類就會被實例化那么他們是同生共死的關系,他們就是組合關系
private Head head = new Head();
}
UML類圖 + PlantUML代碼
@startuml
/'聲明'/
class IDCard
class Head
class Person{
- IDCard idCard
- Head head = new Head()
}
Person o-- IDCard /'IDCard聚合到Person'/
Person *-- Head /'Head組合到Person'/
@enduml
應用案例2
Java代碼
public class Mouse {}
public class Monitor {}
///Computer被創建時,Mouse和Monitor會馬上被new/實例化出來,組合關系
public class Computer被創建時, {
/**鼠標不可以和Computer分離*/
private Mouse mouse = new Mouse();
/**顯示器不可以和Computer分離*/
private Monitor monitor = new Monitor();
public void setMouse(Mouse mouse) {
this.mouse = mouse;
}
public void setMonitor(Monitor monitor) {
this.monitor = monitor;
}
}
UML類圖 + PlantUML代碼
@startuml
/'聲明'/
class Monitor
class Mouse
class Computer{
- Monitor Monitor = new Monitor()
- Mouse mouse = new Mouse)(
}
Computer *-- Monitor /'Monitor聚合到Computer'/
Computer *-- Mouse
@enduml
特殊情況
但是如果在程序中Person實體中定義了對IDCard進行<b style="color:red">級聯刪除</b>,即刪除Person時 連同IDCard一起刪除,那么IDCard 和 Person 就是組合了.