概述UML类图

UML 类图是系统分析和设计阶段的重要产物,是系统编码和测试的重要模型,它用来描述系统中类的内部结构、类与类之间的关系,可以简化人们对系统的理解 。

此处主要对类与类之间的关联关系做介绍。类与类之间的关系包括: 泛化(Generalization)、关联(Association)、聚合(Aggregation)、组合(Composition)、依赖(Dependence) 、实现(Realization)

泛化关系(Generalization),也叫继承关系。

1.类图

属性和方法前的符号意义:

  • -:private
  • +:public
  • #:protected
  • *:package

2.代码
public class Animal {
    
}

public class Dog extends Animal{
    
}

关联(Association)

1.类图

2.代码
//单一关联
public class Dog {
   private List<Home> home;
}

//双向关联
public class Tree {
   private Bird bird;
}
public class Bird {
   private Tree tree;
}

//自关联
public class Dolls {
   private Dolls dolls;
}

聚合(Aggregation)

1.类图

聚合关系是一种特殊的关联关系,聚合关系强调的是整体和部分的关系,其中部分可以脱离整体而存在。

2.代码
//鸟和树都是可独立存在的对象
public class Bird {
   private Tree tree;
}

组合(Composition)

1.类图

组合关系也是一种特殊的关联关系,它与聚合关系很像,也是强调整体与部分的关系,不同的是部分无法脱离整体存在。

2.代码
//牙齿是狗的一部分
public class Dog {
   private Teeth teeth = new Teeth();

}

依赖(Dependence)

1.类图

依赖关系是一种使用关系,它是对象之间 耦合度最小 的一种关联方式,它是一种临时性的关联,在代码中某个类通过局部变量方法的参数、或者静态方法的调用来使用另外一个类的某些方法来完成一些功能

2.代码
public class Water {
   public Boolean drink() {
      return null;
   }

}

public class Bird {
   public Boolean alive(Water water) {
      return null;
   }

}

实现(Realization)

1.类图

2.代码
public interface Postman {
   Boolean receive(Object obj);
   Boolean send(Object obj);
}

public class LogisticsSystem implements Postman {
   public Boolean receive(Object obj) {
      return null;
   }
   
   public Boolean send(Object obj) {
      return null;
   }
}

public class Email implements Postman {
   public Boolean receive(Object obj) {
      return null;
   }
   
   public Boolean send(Object obj) {
      return null;
   }
}
posted @ 2022-02-28 16:12  howard4  阅读(130)  评论(0编辑  收藏  举报