如何将UML类图-UML Class Diagram为一个?
摘要:类图以可视化的方式呈现了软件中的基本单元以及它们之间的关系。在软件架构设计过程中,它可以帮助架构师快速构思项目框架而无需关注代码细节;在软件开发过程中,它又可以精确描述类中的所有元素细节。因此对于开发人员,了解了面向对象就必须掌握UML类图
.wj_nav { display: inline-block; width: 100%; margin-top: 0; margin-bottom: 0.375rem }
.wj_nav_1 { padding-left: 1rem }
.wj_nav_2 { padding-left: 2rem }
.wj_nav span { display: inline-block; margin-right: 0.375rem; color: rgba(102, 102, 102, 1) }
.wj_nav a, .wj_nav a:link, .wj_nav a:visited { color: rgba(51, 51, 51, 1); text-decoration: underline }
.wj_nav a:hover { color: rgba(255, 102, 0, 1); text-decoration: none }
.wj_title_1 { display: inline-block; width: 100%; margin-bottom: 1rem; border-left: 0.375rem solid rgba(255, 102, 0, 1); background-color: rgba(232, 232, 232, 1); font-size: 1.5rem; padding: 0.3125rem 0.625rem }
.wj_title_2 { display: inline-block; width: 100%; font-size: 1.25rem; font-weight: bold; margin-bottom: 1rem }
.wj_title_3 { display: inline-block; width: 100%; font-size: 1rem; font-weight: bold; margin-bottom: 1rem }
.wj_cont { line-height: 180%; margin-bottom: 1rem; font-size: 1rem }
.wj_img { display: inline-block; width: 100%; margin-bottom: 1rem }
.wj_code { margin-top: 0 !important; margin-bottom: 1rem !important; font-size: 0.875rem !important }
.wj_table { border: 1px solid rgba(255, 136, 56, 1); border-collapse: collapse; margin-bottom: 1rem }
.wj_table tr { display: table-row; vertical-align: inherit }
.wj_table tr th { display: table-cell; vertical-align: inherit; font-weight: normal; border: 1px solid rgba(255, 136, 56, 1); padding: 5px 10px; background-color: rgba(255, 102, 0, 1) !important; color: rgba(255, 255, 255, 1); text-align: left }
.wj_table tr td { display: table-cell; vertical-align: inherit; border: 1px solid rgba(255, 136, 56, 1); padding: 0.3125rem 0.625rem; background-color: rgba(255, 255, 255, 1); text-align: left }
.wj_link, .wj_link:link, .wj_link:visited { color: rgba(51, 51, 51, 1); text-decoration: underline }
.wj_link:hover, .wj_link:active { color: rgba(255, 102, 0, 1) }
.wj_watermark, .wj_watermark :link, .wj_watermark :visited, .wj_watermark :hover, .wj_link:active { color: rgba(255, 255, 255, 1) !important; cursor: default !important }
.wj_important { font-weight: bold; color: rgba(255, 102, 0, 1) }
导航
1前言
2UML类图中的元素
2.1类
2.2接口
3UML类图中的关系
3.1关联关系
3.2泛化关系
3.3实现关系
3.4聚合关系
3.5组合关系
3.6依赖关系
3.7关系中的数量表示
1 前言
在面向对象软件开发中,类是软件设计的基本单元,由类构造出对象,由对象支持整个软件系统的运行,因此UML中的类图是开发人员使用最高频的图。一个复杂的软件系统,通常由非常多的类构成,如何让这些相互交织的类与对象高效的协同工作,一直以来都是软件设计领域中的巨大挑战,在这个过程中,诞生了诸如领域驱动设计(DDD),微服务等一系列革命性的思想。
类图以可视化的方式呈现了软件中的基本单元以及它们之间的关系。在软件架构设计过程中,它可以帮助架构师快速构思项目框架而无需关注代码细节;在软件开发过程中,它又可以精确描述类中的所有元素细节。因此对于开发人员,了解了面向对象就必须掌握UML类图。来源:https://www.wubayue.com
2 UML类图中的元素
2.1 类
类的基本构成
在UML类图中,类主要由三部分构成:类的名称、类的属性与类的方法。
类的基本构成代码示例如下:
// 类名称
public class MyClass
{
// 属性
public int MyProperty { get; set; }
// 方法
public int MyMethod(int p1, int p2)
{
return p1 + p2;
}
}
类成员的可访问性
类成员的可访问性通过类成员的前缀符号表示:
+ 表示公有,该成员可以被任何对象访问。
~ 表示包内公有,该成员可以被同一包内的对象访问。
# 表示受保护,该成员只能被当前类对象及子类的对象访问。
- 表示私有,该成员只能被当前类对象访问,其它外部对象无法访问。
类成员的可访问性代码示例如下:
// 类成员的可访问性
public class MyClass
{
// 公有
public void PublicMethod()
{
}
// 包内公有
internal void InternalMethod()
{
}
// 受保护
protected void ProtectedMethod()
{
}
// 私有
private void PrivateMethod()
{
}
}
抽象类与抽象成员
抽象类与抽象成员均使用斜体表示。
抽象类与抽象成员代码示例如下:
// 抽象类
public abstract class AbstractClass
{
// 抽象方法
public abstract void AbstractMethod();
}
静态类成员
静态类成员使用下划线表示。
静态类成员代码示例如下:
// 静态类成员
public class MyClass
{
// 静态属性
public static int StaticProperty { get; set; }
// 静态方法
public static void StaticMethod()
{
}
}
2.2 接口
接口在UML图例中与类的主要差别是接口名称后多了一个圆圈。
接口代码示例如下:来源:https://www.wubayue.com
// 接口
public interface MyInterface
{
// 属性
int MyProperty { get; set; }
// 方法
void MyMethod();
}
3 UML类图中的关系
3.1 关联关系
关联关系是对象之间最常见的一种关系,主要表示为拥有。比如老师拥有课程,老师与课程之间即为单向的关联关系;老师拥有学生,学生也拥有老师,老师与学生之间即为双向的关联关系。
关联关系示例代码如下:
// 课程
public class Course
{
public string name { get; set; }
}
// 老师
public class Teacher
{
// 老师的课程
public Course[] Courses { get; set; }
// 老师的学生
public Student[] Students { get; set; }
}
// 学生
public class Student
{
// 学生的课程
public Course[] Courses { get; set; }
// 学生的老师
public Student[] Students { get; set; }
}
3.2 泛化关系
泛化通常表示子类对父类的继承关系。
泛化关系示例代码如下:
// 动物
public class Animal
{
// 重量
public decimal Weight { get; set; }
// 移动
public virtual void Move()
{
Console.WriteLine("动物都会动");
}
}
// 狗
public class Dog : Animal
{
public override void Move()
{
Console.WriteLine("狗在地上跑");
}
}
// 鱼
public class Fish : Animal
{
public override void Move()
{
Console.WriteLine("鱼在水中游");
}
}
3.3 实现关系
通常为具体类实现抽象的接口。
实现关系示例代码如下:
// 画画
public interface Drawing
{
// 笔
string Pen { get; }
// 作画
void Paint();
}
// 素描画
public class Sketch : Drawing
{
public string Pen
{
get { return "铅笔"; }
}
public void Paint()
{
Console.WriteLine("画素描画");
}
}
// 水彩画
public class Watercolor : Drawing
{
public string Pen
{
get { return "毛笔"; }
}
public void Paint()
{
Console.WriteLine("画水彩画");
}
}
3.4 聚合关系
表示整体与部分的关系,聚合属于强关联关系。
3.5 组合关系
表示整体与部分的关系,组合属于最强关联关系。
组合与聚合的区别在于组合中的部分不能脱离于整体单独存在,比如部门不能脱离于公司而存在。
3.6 依赖关系
依赖关系并非类图中的特有关系,而是UML中的一种通用关系,比如在用例图中也能使用。依赖即使用关系,通常为单方向。
3.7 关系中的数量表示
两个类之间的关系,有时还需要使用数量进行更精确的描述,比如一辆汽车有一台引擎多个轮胎,一家公司有多个部门等。类图关系中的数量表示位于关系连线的两端,具体含义如下:来源:https://www.wubayue.com
标识含义
0..1
0个或1个,最多1个
0..*
0个或多个,数量不限
1..1
只有1个
1..*
1个或多个,至少1个
1
只有1个(简化表示)
*
0个或多个,数量不限(简化表示)
本文同步发布于个人博客:wubayue.com
<全文完>
