UML class diagrams

Lucid Software
10 Aug 202312:24

Summary

TLDRこのビデオスクリプトでは、UMLクラスダイアグラムの基本特性から始まり、クラスの属性、メソッド、可視性、そして継承、関連性、集約、合成などのクラス間の関係について学びます。動物園の例を使って、これらの概念を説明し、さらにオンラインショッピングカートの実際の例を通じて、クラス間の多重度やその他の関係を理解します。最後に、Lucidchartを使用したUMLクラスダイアグラムの作成方法も紹介しています。

Takeaways

  • 📚 UMLクラス図は、システム内の異なる要素をクラスとして表し、それらの関係性を可視化するツールです。
  • 🐾 クラスは、動物園の動物を例にすると、属性(name, id, ageなど)とメソッド(setName, eatなど)で特徴化されます。
  • 🔑 属性とメソッドには可視性があり、プライベート(-)、パブリック(+)、プロテクテッド(#)、デフォルト(〜)の4つがあります。
  • 👶 継承は、スーパークラス(动物)の属性とメソッドをサブクラス(亀、水獺、 lorisなど)が継承する関係を表します。
  • 🔄 抽象化は、動物クラスのように、システム内でインスタンス化されることがないが、コードの再利用を促進するクラスを指します。
  • 🔗 関連は、オッターと海うにのような単純な関係を表し、依存関係はなく、単純にオッターが海うにを食べるだけです。
  • 💬 集約は、亀の群れ(creep)と亀の関係のように、部分が全体から独立して存在できる関係を表します。
  • 🏠 構成は、ビジターセンターとロビー、トイレの関係のように、部分が全体から独立して存在できない関係を表します。
  • 🔢 多重性は、関係における数値の制約を設定し、1..1、0..1、0..*、1..*など様々なタイプがあります。
  • 🛒 オンラインショッピングカートのUMLクラス図は、実際のシステムにおけるクラスと関係性の例を示しており、ユーザー、顧客、管理者のクラスが含まれています。
  • 🔍 Lucidchartなどのダイアグラムアプリケーションを使用することで、UMLクラス図の作成がより簡単かつ直感的になります。

Q & A

  • UMLクラスダイアグラムとは何ですか?

    -UMLクラスダイアグラムは、ソフトウェア開発においてクラスとその関係を視覚化する図記法です。オブジェクト指向プログラミングにおいては、システムの構造を理解するための重要なツールです。

  • クラスダイアグラムでクラスはどのように表されますか?

    -クラスダイアグラムでは、クラスは長方形で表され、クラス名が上部に書かれ、属性が中央に、メソッドが下部に配置されます。

  • 属性とは何で、どのように表すのですか?

    -属性は、クラスのインスタンスを特定する重要なデータの一部であり、フィールド、変数、プロパティとも呼ばれます。属性は中央のセクションに表示され、可視性、属性名、データ型の順で書きます。

  • メソッドとは何で、どのように表すのですか?

    -メソッドは、クラスの振る舞いを指定する機能であり、操作や関数とも呼ばれます。メソッドは下部のセクションに表示され、可視性、メソッド名、パラメータ(必要に応じて)が書かれます。

  • 可視性とは何で、どのような種類がありますか?

    -可視性は、属性やメソッドへのアクセスを制御するものです。プライベート(-)、パブリック(+)、プロテクテッド(#)、パッケージ(〜)、抽象(イタリック)があります。

  • 継承関係とは何で、どのように表すのですか?

    -継承関係は、あるクラス(スーパークラス)の属性やメソッドを他のクラス(サブクラス)が継承する関係です。継承関係は、オープンな矢印で表されます。

  • 関連関係とは何で、どのように表すのですか?

    -関連関係は、クラス間の基本的な関係を表すもので、単純な線で表されます。例えば、オッターが海うにを食べるという関係です。

  • 集約関係と合成関係の違いは何ですか?

    -集約関係は、全体とその部分の関係を表す特殊な関連関係であり、部分が全体の外部で存在できることを示します。一方、合成関係では、部分は全体の外部で存在できないことを示します。

  • 多重性とは何で、どのように表すのですか?

    -多重性は、関係における数的限制を表すもので、例えば「1」「0..1」「*」「1..N」「N..M」などがあります。これは、クラス間の関係において、特定のクラスが他クラスと関連する数を示します。

  • 実際のオンラインショッピングカートの例では、クラス間の継承関係や合成関係はどのように表されていますか?

    -オンラインショッピングカートの例では、顧客クラスと管理者クラスがユーザークラスから継承しています。また、顧客クラスからショッピングカート、配送情報、注文詳細への合成関係が示されており、これらの部分は顧客クラスの外部で存在できないことを表しています。

  • Lucidchartとは何で、どのように使いますか?

    -Lucidchartは、UMLクラスダイアグラムを含むさまざまな図を作成するための図表アプリケーションです。メールアドレスで無料でアカウントを作成し、クラスダイアグラムを作成することができます。

  • このチュートリアルの最後に提供されるリンクはどのようなものですか?

    -このチュートリアルの最後に提供されるリンクは、トレーニングのウェブサイト「training.lucid.co」へのリンクで、図表、プロセス、システム、組織に関するさらに学習に興味がある場合にアクセスできます。

Outlines

00:00

📚 UMLクラス図の基本

この段落では、UMLクラス図の基本的な特徴について解説しています。動物園のシステムを例に、クラスの表現方法や属性、メソッド、可視性(private, public, protected, package)について説明しています。クラス図を作成する際には、図形アプリケーションを使うことが有効で、Lucidchartを使ったデモンストレーションが行われています。

05:02

🔄 クラス間の関係性

継承、関連、集約、合成、および多重性について学びます。動物園の例を使って、継承はクラス同士の共通属性を共有する方法であり、関連はクラス間の単純な関係を表す方法、集約は部分が全体から独立して存在できる関係、合成は部分が全体から独立して存在できない関係を表す方法を説明しています。また、多重性は関係における数的限制を表す方法です。

10:04

🛒 オンラインショッピングカートのUMLクラス図

実際の例としてオンラインショッピングカートのUMLクラス図を分析しています。ユーザークラス、顧客クラス、管理者クラスの属性とメソッド、継承関係、そして顧客クラスとショッピングカート、配送情報、注文詳細の間の合成関係について説明しています。また、顧客が注文を持つことができる多重性や、注文が顧客に属する1対1の関係についても触れています。

Mindmap

Keywords

💡UMLクラス図

UMLクラス図は、オブジェクト指向ソフトウェア開発においてクラスとその関係を視覚化する図式です。このビデオでは、UMLクラス図の基本特性やクラス間の関係性について説明し、動物園の例を使って具体的なクラスと属性、メソッドを紹介しています。

💡クラス

クラスは、UMLクラス図においてシステム内のものを表す単位です。クラスは属性とメソッドを持ち、特定のインスタンスを特定するためのデータを含むことができます。ビデオでは動物クラスを例に、動物の属性として名前、ID、年齢を挙げています。

💡属性

属性は、クラスのインスタンスを特定するための重要なデータの一部であり、フィールド、変数、プロパティとも呼ばれます。ビデオでは、動物クラスの属性として名前、ID、年齢が挙げられており、これらは各動物の特定の値を保持します。

💡メソッド

メソッドとは、クラスの動作特徴を指定する関数であり、操作や機能を表します。ビデオでは、動物クラスのメソッドとしてsetName(名前の変更)やeating(食べる動作)が紹介されており、これらはクラスの振る舞いを定義しています。

💡可視性

可視性は、属性やメソッドに対するアクセスの制限を表します。ビデオでは、プライベート(-)、パブリック(+)、プロテクテッド(#)、パッケージ(〜)の可視性を説明しており、これにより他のクラスやサブクラスがどのように属性やメソッドにアクセスするかを制御できます。

💡継承

継承は、クラス間の関係性の一種で、サブクラスがスーパークラスの属性とメソッドを継承できるようにします。ビデオでは動物園の亀、カワウソ、スローローリスのクラスが動物クラスから継承している例を通じて継承の概念が説明されています。

💡抽象クラス

抽象クラスは、具体的なインスタンス化ができないクラスであり、他のクラスの基底として使われます。ビデオでは動物クラスが抽象クラスとして位置づけられており、これはシステム内で直接インスタンス化されることなく、他のクラスの基礎として機能します。

💡関連

関連は、クラス間の単純な関係を表すもので、クラス同士がどのように相互作用するかを示します。ビデオではカワウソがウニを食べるという関連関係が紹介されており、これはクラス間の基本的な相互作用を表しています。

💡集約

集約は、関連の一種で、全体とその部分との関係を示します。ただし、部分は全体から独立して存在することができます。ビデオでは、ツルモグリのクラスがツルモグリのグループであるクリープと集約関係にある例が挙げられています。

💡組成

組成は、集約と似ていますが、部分が全体から独立して存在できないという点が異なります。ビデオでは、ビジターセンターがロビーとトイレと組成関係にある例が紹介されており、これは部分が全体なしには存在できないことを示しています。

💡多重性

多重性は、クラス間の関係における数値的な制約を設定する方法です。ビデオでは、ビジターセンターが1つのロビーを持つべきであり、トイレは1つ以上を持つことができるという多重性を説明しています。

💡オンラインショッピングカート

ビデオの後半では、オンラインショッピングカートのUMLクラス図が紹介されており、これはリアルワールドの例として、ユーザー、顧客、管理者、ショッピングカート、注文などのクラスとその関係性について理解を深めるのに役立ちます。

Highlights

介绍UML类图的基本特性。

使用动物园系统作为类图示例,帮助理解概念。

类通过特定的形状表示,例如动物类。

属性用于标识类的每个实例,如动物的名称、ID和年龄。

属性的格式包括可见性、名称、数据类型。

方法定义类的任何行为特征,如动物的setName和eating方法。

方法的格式包括可见性、名称和函数的参数。

可见性定义属性或方法的可访问性,如私有、公共、受保护。

使用Lucidchart软件绘制UML类图。

继承关系允许子类继承超类的属性和方法。

抽象类用于简化代码,避免重复,如动物类。

关联关系表示类之间的基本联系,如海獭和海胆。

聚合关系表示整体和部分的关系,如龟群和龟。

组合关系表示部分不能独立于整体存在,如访客中心和其房间。

多重性设置关系的数量限制,如访客中心的大厅和浴室数量。

在线购物车系统的UML类图实例分析。

用户类、客户类和管理员类之间的关系和属性。

组合关系在客户类中的体现,如购物车和订单。

多重性在客户和订单关系中的应用。

教程结束,鼓励访问training.lucid.co了解更多。

Transcripts

play00:00

Hi, my name is Bear, and today I'll be teaching you about UML class diagrams.

play00:06

We'll start with some of the basic characteristics.

play00:09

Then we'll talk about relationships

play00:11

and finally we'll finish up by going through a few examples together.

play00:16

All right, let's talk about some of the basic characteristics of class diagrams.

play00:20

To help explain these characteristics, I'm going to make up an example

play00:24

to help us illustrate.

play00:26

So let's say we're building a system for a zoo.

play00:29

And keep in mind, these examples I'm going to use probably wouldn't

play00:33

ever find their way into an actual program, but humor me

play00:37

as I use these concepts to make it easier to understand.

play00:41

So in our zoo, we want to describe the different things that are in the system.

play00:46

You represent these things through classes,

play00:49

and a class is depicted with this shape here.

play00:53

So what's in a zoo?

play00:54

Well, a ton of animals.

play00:56

So we can create a class for our animals.

play00:58

To do that, you just write the name of the class in this top section.

play01:03

If our class is animal, an instance of that class would be a specific animal.

play01:08

So the question is, how would you go about

play01:11

identifying each instance of that class?

play01:14

You do that through attributes.

play01:17

An attribute is a significant piece of data containing values

play01:21

that describe each instance of that class.

play01:24

They're also known as fields, variables or properties,

play01:27

and they go in the middle section here.

play01:30

So for our animal class, we could create attributes like name, id, and age.

play01:36

That way we could identify a specific instance of the animal class like Ruth

play01:40

id number 304, age 114.

play01:44

These need to be formatted a certain way, though.

play01:46

You'll start with visibility, which we'll talk about later.

play01:49

The name of the attribute, beginning with a lowercase letter.

play01:52

Then you follow it with a colon and the data type for the name.

play01:57

We'd want to return a string

play01:59

and we can format the other attributes the same way, except

play02:03

that we'd want to return an integer, since these are numbers.

play02:06

Now that we've got some attributes

play02:07

for our animal class, we get to the bottom section here.

play02:11

This is where you put methods which are also known as operations or functions.

play02:16

Methods allow you to specify any behavioral features of a class.

play02:20

So we might ask ourselves what are some different behaviors of this animal class?

play02:25

Maybe we'd want to be able

play02:26

to change our animals names, like Ruth should actually be called Rita.

play02:31

So let's create a function called setName.

play02:34

We can also create a method for eating since all of our animals eat.

play02:38

Methods also need to be formatted a certain way.

play02:41

You start with visibility, which we'll talk about next.

play02:44

Then the method beginning with a lowercase letter.

play02:47

Next, you put parentheses to signify the function you're going to program later.

play02:52

You can also add variables and the data type in here,

play02:56

but in most cases it's not really necessary.

play02:59

We'll add visibility in parentheses to the eat method as well.

play03:04

Now let's talk about visibility.

play03:07

The visibility of an attribute or method sets the accessibility

play03:11

for that attribute or method.

play03:12

So right now we have a minus sign for all of these, which indicates

play03:16

that each of these attributes and methods are private.

play03:18

They can't be accessed by any other class or subclass.

play03:23

The exact opposite is the plus sign, which means an attribute

play03:26

or method is public and can be accessed by any other class.

play03:31

Another visibility type is indicated by the hash,

play03:34

which means an attribute or method is protected.

play03:37

These can only be accessed by the same class or its subclasses.

play03:41

And finally there's the tilde or the squiggly, as I like to call it.

play03:46

This sets the visibility to package or default, which means it can be used

play03:50

by any other class as long it’s in the same package.

play03:54

But that one is rarely ever used.

play03:56

In most cases, your attributes are going to be private

play03:59

or protected, and methods are often public.

play04:02

Let's quickly review these basics with another example.

play04:05

Let's make a class for employee.

play04:08

We could give an employee attributes like name,

play04:12

employee ID,

play04:14

phone number

play04:16

and department.

play04:17

We'll want all of these attributes to be private and we could create a simple

play04:22

method like updating a phone number which we'll go ahead and set to public.

play04:27

So you might have noticed that I'm using

play04:30

a diagraming application to create these UML class diagrams.

play04:34

The same principles apply if you're using pen and paper.

play04:37

But a diagraming software makes it much easier.

play04:41

The diagraming application that I'm using today is Lucidchart.

play04:45

You can sign up for a free account with your email address

play04:48

and then you'll be able to follow along as we make these class diagrams together.

play04:52

So the next thing we'll need to cover

play04:54

are the different relationships that exist between classes.

play04:57

The first type of relationship that we'll describe is inheritance.

play05:01

And I'm going to keep going with the zoo example

play05:03

because it makes it easy to understand the logic of these relationships.

play05:08

We'll get to a more

play05:09

technical real world example just a little later.

play05:12

Okay.

play05:12

So inheritance, let's say, in our zoo, the only animals we have

play05:17

are tortoises, otters and the lesser known but nonetheless amazing slow loris.

play05:23

In our system, we want to distinguish each of them as their own class.

play05:27

So we'll make three classes for a tortoise, otter, and slow loris.

play05:32

Now, instead of duplicating attributes for name, id and age,

play05:36

we can make these classes into subclasses of the animal class

play05:40

by drawing open arrows like this.

play05:43

This is an inheritance relationship.

play05:46

We're saying that these subclasses inherit all the attributes

play05:49

and methods of the superclass.

play05:51

You could also use the terms child and parent class.

play05:55

So our order class is going to inherit the attributes of name, age and ID.

play05:59

And then we could also add an attribute specific to the otter like whisker length.

play06:04

One of the advantages of inheritance is that if we wanted to change or add

play06:08

an attribute for all animals, we wouldn't have to go in and make

play06:11

that change to tortoise and then otter and then slow loris.

play06:16

We just make the change to the animal class

play06:18

and it applies across all of its subclasses.

play06:22

in this scenario, we also have what's called abstraction.

play06:26

Animal is an abstract class because in our system,

play06:29

any time we want to instantiate one of our classes, it's

play06:32

going to be a tortoise, otter, or slow loris.

play06:36

We wouldn't instantiate the animal class itself.

play06:38

The animal class is just a way to simplify things and keep the code dry

play06:42

so you don't repeat yourself.

play06:44

So to show that this is an abstract class,

play06:46

we'll put the name in italics.

play06:49

You could put the class name inside these things as well, but I prefer italics.

play06:54

Okay.

play06:54

Another type of relationship is association.

play06:57

So if we had a class for sea urchin, we could draw an association

play07:01

which is just depicted by a simple line between otter and sea urchin.

play07:05

And we could say otter eats sea urchin.

play07:08

There's no dependency between them.

play07:10

It's just a basic association relationship, and it's pretty simple.

play07:15

The next type of relationship is aggregation.

play07:18

It's a special type of association that specifies a whole and its parts.

play07:23

So to continue with our zoo example,

play07:25

again, this is just to help explain the logic.

play07:29

Let's create a new class for a group of tortoises.

play07:33

A group of tortoises is called a creep.

play07:35

So here's our creep class and it's got a relationship with tortoise.

play07:40

Any of our zoo's tortoises could be part of a creep, but they don't have to be.

play07:44

A tortoise could leave the group at any point and still exist on its own.

play07:49

That type of relationship where a part can exist outside

play07:52

the whole is aggregation, and we note it with an open diamond.

play07:56

There's also a relationship where the part can't exist outside the whole.

play08:01

That's called composition.

play08:03

To illustrate this, I'm going to create a few new classes.

play08:07

Let's just say we have several different visitor centers in our zoo,

play08:11

and each of those visitor centers has a lobby and a bathroom.

play08:15

Now, if one of our visitor centers was torn down, the lobby

play08:18

and the bathroom of that visitor center would also be destroyed.

play08:22

Those rooms couldn't exist apart from the visitor center that is contained in.

play08:27

That’s composition.

play08:29

When a child object wouldn't be able to exist without its parent object.

play08:33

We note a composition relationship with a closed diamond.

play08:38

Another important

play08:39

concept when we talk about relationships in UML is multiplicity.

play08:44

Multiplicity allows you to set numerical constraints on your relationships.

play08:48

For example, let's say we want to specify

play08:51

that our visitor centers are going to have just one lobby.

play08:54

We simply write the number one here, meaning

play08:56

there can be one and only one lobby per visitor center.

play09:00

But for bathrooms, maybe we want to make it so that there's

play09:03

at least one bathroom per visitor center.

play09:06

But leave the option to have as many as you'd like.

play09:09

We'd use this notation to denote one or many bathrooms.

play09:14

Other types of multiplicity are 0 to 1, which is an optional relationship.

play09:18

N representing the specific amount, which in our example was one,

play09:22

but it could be any other number depending on your use case.

play09:26

Zero to many.

play09:27

One to many.

play09:28

Or a specific range.

play09:31

Hopefully our zoo examples have helped explain those concepts.

play09:34

But I want to show you what a real world example would look like.

play09:38

This is a UML class diagram for an online shopping cart.

play09:42

And if you want to look at the diagram with me, just click the link.

play09:45

In the top right corner,

play09:47

you can

play09:48

see that this system has several classes and relationships.

play09:51

So let's walk through a couple of them.

play09:53

We'll start with the user class.

play09:55

It's got attributes for user ID, password, login status and register date.

play10:00

You've got the different return types on the right and on the left.

play10:04

The visibility which is set to private.

play10:07

You can see how the values returned by those attributes

play10:10

would specifically describe an instance of the user class.

play10:14

Down below we have a public method of verify

play10:16

log in returning a boolean and this makes sense.

play10:20

Right? Methods are behaviors of a class.

play10:23

So if you were to log into your user account,

play10:25

there's a function in place that verifies your login credentials.

play10:30

Let's move on to the customer class.

play10:32

This arrow tells us that customer is a child of user,

play10:35

so customer inherits all the attributes and methods of the user class.

play10:39

And same thing for the administrator class.

play10:41

Both of these inherit from user, but also have their own

play10:45

specific attributes and methods

play10:47

like administrator can update the catalog, but the customer can't.

play10:50

Stemming from customer, there are also several lines with the closed end diamond.

play10:55

So if you recall, these are composition relationships

play10:59

which mean that the parts cannot exist without the whole.

play11:02

in the instance of the customer class, if that customer’s account was destroyed,

play11:07

his shopping cart would be destroyed as well and all of his orders would be lost.

play11:12

They can't exist outside of the customer.

play11:15

The same applies for the shipping info and order details.

play11:18

If there's no order, there's not going to be any order details or shipping info.

play11:23

The last thing we'll look at in this example is multiplicity.

play11:26

You can see that a customer can have zero or many orders.

play11:30

Makes sense, right?

play11:31

You could create a customer account for an online store but never buy anything.

play11:37

Or you could be a frequent customer and place several different orders.

play11:41

And then on the flip side, an order can belong to only one customer.

play11:45

It'd be pretty confusing if a specific order with a unique order ID

play11:48

was duplicated across several different customers.

play11:52

And here you can see a 1 to 1 relationship.

play11:55

Each order has one and only one order details

play11:59

and order details belongs to one and only one order.

play12:04

Okay, that wraps up our tutorial for UML class diagrams.

play12:08

If you're interested in learning more about diagraming, processes, systems

play12:13

and organizations, visit training.lucid.co I hope to see you there.

Rate This

5.0 / 5 (0 votes)

関連タグ
UMLクラス図チュートリアル継承関連性属性メソッド抽象化アソシエーションアグリゲーションコンポジション多重性