画UML图和写文章差不多,都是把自己的思想描述给别人看,关键在于思路和条理,UML图分类:
说明:类图是描述类与类之间的关系的,是UML图中最核心的
public class Person{
private Integer id;
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}
对应类图如下:
@startuml
class Person {
private Integer id;
private String name;
public void setName(String name)
public String getName()
}
@enduml
只要是在类中用到了对方,那么他们之间就存在依赖关系。如果没有对方,连编译都通过不了。
示例:
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();
}
}
public class PersonDao {
}
public class Person {
}
public class IDCard {
}
public class Department {
}
类图:
@startuml
class PersonServiceBean {
private PersonDao personDao;
public void save(Person person)
public IDCard getIDCard(Integer personid)
public void modify()
}
class PersonDao{}
class Person{}
class IDCard{}
class Department{}
PersonDao <.. PersonServiceBean:依赖关系
Person <.. PersonServiceBean:依赖关系
IDCard <.. PersonServiceBean:依赖关系
Department <.. PersonServiceBean:依赖关系
@enduml
泛化关系实际上就是继承关系,他是依赖关系的特例
示例:
public abstract class DaoSupport {
public void save(Object entity){
}
public void delete(Object id){
}
}
public class PersonServiceBean extends DaoSupport{
}
类图:
@startuml
abstract class DaoSupport {
public void save(Object entity)
public void delete(Object id)
}
class PersonServiceBean
DaoSupport <|-- PersonServiceBean:泛化关系
@enduml
实现关系实际上就是A类实现B类,他是依赖关系的特例
public interface PersonService {
public void delete(Integer id);
}
public class PersonServiceBean implements PersonService{
@Override
public void delete(Integer id) {
}
}
类图:
@startuml
interface PersonService {
public void delete(Integer id);
}
class PersonServiceBean{
public void delete(Integer id);
}
PersonService <|.. PersonServiceBean:实现关系
@enduml
关联关系实际上就是类与类之间的联系,他是依赖关系的特例
关联具有导航性:即双向关系或单向关系
关系具有多重性:如“1”(表示有且仅有一个),“0…”(表示0个或者多个),“0,1”(表示0个或者一个),“n…m”(表示n到m个都可以),“m…”(表示至少m个)
单向一对一关系:
public class Person{
private IDCard card;
}
public class IDCard{}
类图:
@startuml
class Person {
private IDCard card;
}
class IDCard
Person"1"-->"1"IDCard:单向一对一
@enduml
public class Person{
private IDCard card;
}
public class IDCard{
private Person person;
}
类图:
@startuml
class Person {
private IDCard card;
}
class IDCard{
private Person person;
}
Person"1"<-->"1"IDCard:双向一对一
@enduml
聚合关系(Aggregation)表示的整体和部分的关系,整体与部分可以分开,聚合关系是关联关系的特例,所有他具有关联的导航性与多重性。
如:一台电脑由键盘(keyboard)、显示器(monitor),鼠标等组成;组成电脑的各个配件是可以从电脑上分离出来的,使用带空心菱形的实线来表示:
public class Computer {
private Monitor monitor;
private Mouse mouse;
private Keyboard keyboard;
public void setMonitor(Monitor monitor) {
this.monitor = monitor;
}
public void setMouse(Mouse mouse) {
this.mouse = mouse;
}
public void setKeyboard(Keyboard keyboard) {
this.keyboard = keyboard;
}
}
public class Keyboard {
}
public class Monitor {
}
public class Mouse {
}
类图:
@startuml
class Computer {
private Monitor monitor;
private Mouse mouse;
private Keyboard keyboard;
public void setMonitor(Monitor monitor)
public void setMouse(Mouse mouse)
public void setKeyboard(Keyboard keyboard)
}
class Monitor
class Mouse
class Keyboard
Computer o-- Monitor:聚合关系
Computer o-- Mouse:聚合关系
Computer o-- Keyboard:聚合关系
@enduml
组合关系:也是整体和部分的关系,但是整体与部分不可以分开。
示例: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 {
}
类图:
@startuml
class Person {
private IDCard card;
private Head head=new Head();
}
class Head {
}
class IDCard {
}
Person o-- IDCard:聚合关系
Person *-- Head:组合关系
@enduml