UML 基本介紹
- UML——Unified modeling language UML (統一模組化語言),是一種用于軟體系統分析和設計的語言工具,它用于幫助軟體開發人員進行思考和記錄思路的結果
- UML 本身是一套符号的規定,就像數學符号和化學符号一樣,這些符号用于描述軟體模型中的各個元素和他們之間的關系,比如類、接口、實作、泛化、依賴、組合、聚合等,如下圖:

使用工具
- AmaterasUML(自己去網上找教程)
- 使用UML 來模組化,常用的工具有Rational Rose , 也可以使用一些插件來模組化
UML 圖
畫UML 圖與寫文章差不多,都是把自己的思想描述給别人看,關鍵在于思路和條理,UML 圖分類:
- 用例圖(use case)
- 靜态結構圖:類圖、對象圖、包圖、元件圖、部署圖
- 動态行為圖:互動圖(時序圖與協作圖)、狀态圖、活動圖
說明:
- 類圖是描述類與類之間的關系的,是UML 圖中最核心的
類圖—依賴關系(Dependence)
- 說明: 隻要是在類中用到了對方,那麼他們之間就存在依賴關系。如果沒有對方,連編繹都通過不了。
public class PersonServiceBean {
private PersonDao personDao;//類
public void save(Person person){}
public IDCard getIDCard(Integer personid){}
public void modify(){
Department department = new Department();
}
}
public class PersonDao{}
public class IDCard{}
public class Person{}
public class Department{}
- 對應的類圖:PersonServiceBean 依賴于PersonDao,IDCard,Person,Department類
UML類圖(依賴,聚合,泛化,關聯,聚合,組合)UML 基本介紹類圖—依賴關系(Dependence)類圖—泛化關系(generalization)類圖—實作關系(Implementation)類圖—關聯關系(Association)類圖—聚合關系(Aggregation)類圖—組合關系(Composition)總結
小結
1) 類中用到了對方
2) 如果是類的成員屬性
3) 如果是方法的傳回類型
4) 是方法接收的參數類型
5) 方法中使用到
類圖—泛化關系(generalization)
- 泛化關系實際上就是繼承關系,他是依賴關系的特例
public abstract class DaoSupport{
public void save(Object entity){
}
public void delete(Object id){
}
}
public class PersonServiceBean extends Daosupport{
}
2.對應的類圖
小結:
- 泛化關系實際上就是繼承關系
- 如果A 類繼承了B 類,我們就說A 和B 存在泛化關系
類圖—實作關系(Implementation)
- 實作關系實際上就是A 類實作B 接口,他是依賴關系的特例
public interface PersonService {
public void delete(Interger id);
}
public class PersonServiceBean implements PersonService {
public void delete(Interger id){}
}
- =>類圖
UML類圖(依賴,聚合,泛化,關聯,聚合,組合)UML 基本介紹類圖—依賴關系(Dependence)類圖—泛化關系(generalization)類圖—實作關系(Implementation)類圖—關聯關系(Association)類圖—聚合關系(Aggregation)類圖—組合關系(Composition)總結
小結
- 實作關系就是實作
類圖—關聯關系(Association)
關聯關系實際上就是類與類之間的聯系,他是依賴關系的特例
關聯具有導航性:即雙向關系或單向關系
關系具有多重性:如“1”(表示有且僅有一個),“0…”(表示0個或者多個), “0,1”(表示0個或者一個),“n…m”(表示n到 m個都可以),“m…*”(表示至少m個)。
//單向一對一關系
public class Person {
private IDCard card;
}
public class IDCard{ }
//雙向一對一關系
public class Person {
private IDCard card;
}
public class IDCard{
private Person person
}
類圖:
類圖—聚合關系(Aggregation)
基本介紹
聚合關系(Aggregation)表示的是整體和部分的關系,整體與部分可以分開。聚合關系是關聯關系的特例,是以他具有關聯的導航性與多重性。
如:一台電腦由鍵盤(keyboard)、顯示器(monitor),滑鼠等組成;組成電腦的各個配件是可以從電腦上分離出來的,使用帶空心菱形的實線來表示:
應用執行個體
public class Computer {
private Mouse mouse; //滑鼠可以和computer分離
private Moniter moniter;//顯示器可以和Computer分離
public void setMouse(Mouse mouse) {
this.mouse = mouse;
}
public void setMoniter(Moniter moniter) {
this.moniter = moniter;
}
}
public class Moniter {
}
public class Mouse {
}
對應類圖
類圖—組合關系(Composition)
基本介紹
組合關系:也是整體與部分的關系,但是整體與部分不可以分開。
再看一個案例:在程式中我們定義實體:Person 與IDCard、Head, 那麼Head 和Person 就是組合,IDCard 和Person 就是聚合。
但是如果在程式中Person 實體中定義了對IDCard 進行級聯删除,即删除Person 時連同IDCard 一起删除,那麼IDCard 和Person 就是組合了.
箭頭指向組成類
應用案例
public class Person{
private IDCard card;
private Head head = new Head();
}
public class IDCard{}
public class Head{}
對應的類圖:
案例2:
public class Computer {
private Mouse mouse = new Mouse(); //滑鼠可以和computer 不能分離
private Moniter moniter = new Moniter();//顯示器可以和Computer 不能分離
public void setMouse(Mouse mouse) {
this.mouse = mouse;
}
public void setMoniter(Moniter moniter) {
this.moniter = moniter;
}
}
public class Mouse {
}
public class Moniter {
}
對應的類圖
總結
依賴關系:隻要是在類中用到了對方,那麼他們之間就存在依賴關系。如果沒有對方,連編繹都通過不了。
泛化關系:泛化關系實際上就是繼承關系,他是依賴關系的特例
實作關系:實作關系實際上就是A 類實作B 接口,他是依賴關系的特例
關聯關系:關聯關系實際上就是類與類之間的聯系,他是依賴關系的特例
聚合關系(Aggregation):表示的是整體和部分的關系,整體與部分可以分開。聚合關系是關聯關系的特例,是以他具有關聯的導航性與多重性。
組合關系:也是整體與部分的關系,但是整體與部分不可以分開。