You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: mod-5-oop/5-has-many-belongs-to.md
+28-36Lines changed: 28 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,15 @@
1
-
# Has Many/Belongs To
1
+
# UML Diagrams and Has Many/Belongs To Relationships
2
2
3
3
{% hint style="info" %}
4
4
Follow along with code examples [here](https://github.com/The-Marcy-Lab-School/5-1-0-has-many-belongs-to)!
5
5
{% endhint %}
6
6
7
7
**Table of Contents:**
8
8
-[Intro: Class Diagrams](#intro-class-diagrams)
9
-
-[Practice](#practice)
10
-
-[Entity Relationships](#entity-relationships)
11
-
-[Make some has many / belongs to class relationships](#make-some-has-many--belongs-to-class-relationships)
9
+
-[Practice: The Book Class](#practice-the-book-class)
10
+
-[Has Many / Belongs To Relationships Between Classes](#has-many--belongs-to-relationships-between-classes)
11
+
-[Practice: The Library Class](#practice-the-library-class)
12
+
-[Challenge](#challenge)
12
13
13
14
## Intro: Class Diagrams
14
15
@@ -23,7 +24,7 @@ UML stands for **U**nified **M**odeling **L**anguage and it defines a way of des
23
24
24
25

25
26
26
-
## Practice
27
+
###Practice: The Book Class
27
28
28
29
UML Diagrams can be created using a tool like [https://draw.io](https://draw.io) or they can simply be drawn using pen and paper.
29
30
@@ -52,7 +53,19 @@ class Book {
52
53
}
53
54
```
54
55
55
-
Now, to the right of your `Book` diagram, create a diagram for the `Library` class below:
56
+
## Has Many / Belongs To Relationships Between Classes
57
+
58
+
Often, classes will interact with each other. The way in which they interact defines what kind of relationship exists between the two classes.
59
+
60
+
One of the most common relationships is a **has-many / belongs to** relationship in which one class manages instances of another. For example, a `PetOwner` class might manage many instances of a `Cat` class.
61
+
62
+

63
+
64
+
We can turn our diagrams from simple class diagrams to **Entity Relationship Diagrams** (ERDs) by connecting them.
65
+
66
+
### Practice: The Library Class
67
+
68
+
Now, imagine that in addition to our `Book` class, we had a `Library` class. Every instance of the `Library` class might manage its own collection of `Book` instances.
56
69
57
70
```js
58
71
classLibrary {
@@ -69,15 +82,18 @@ class Library {
69
82
70
83
// Library Instance Methods
71
84
addBook(title, author, genre) {
85
+
// When adding a book to the Library, a new Book instance is created.
72
86
constaddedBook=newBook(title, author, genre);
87
+
// The Book is added to this library's collection, forming the "has many/belongs to" relationship
<summary><strong>Book and Library UML Diagrams</strong></summary>
96
-
97
-
In this diagram, we take it a step further and define the type of each property, method parameter, and returned value of each method. This is called the **signature** of a property/method.
Class diagrams can show the data and functionality of a class, but the relationships between classes is just as important. We can turn our diagrams from simple class diagrams to **Entity Relationship Diagrams** (ERDs) by connecting them.
108
-
109
-
There are many types of relationships, and many ways to represent them. Below is a common way to represent relationships between classes:
110
-
111
-
* "Has many / belongs to" (a.k.a. "one to many")
112
-
* "Is A" (a.k.a. "Inheritance")
113
-
114
-

115
-
116
-
<details>
117
-
118
-
<summary><strong>Q: What is the relationship between the <code>Library</code> and <code>Book</code> classes?</strong></summary>
111
+
<summary><strong>Q: What is the relationship between a <code>Library</code> and a <code>Book</code> class?</strong></summary>
119
112
120
113
A library has many books. A book belongs to a Library
121
114
122
-
</details>
123
-
115
+
Later this week, we'll learn how to implement an "Is A" relationship with the `extends` keyword.
124
116
117
+
</details>
125
118
126
-
Later this week, we'll learn how to implement an "Is A" relationship with the `extends` keyword.
127
119
128
-
**TODO:**Draw the correct association line between your two classes. If you are using draw.io, go to the "ERD" section and find the "one-to-many" connector
120
+
**TODO:**Now, create a UML diagram for the `Library` class and draw the correct association line between your two classes. If you are using draw.io, go to the "ERD" section and find the "one-to-many" connector
129
121
130
122
<details>
131
123
@@ -137,7 +129,7 @@ Later this week, we'll learn how to implement an "Is A" relationship with the `e
137
129
138
130
139
131
140
-
## Make some has many / belongs to class relationships
132
+
## Challenge
141
133
142
134
Below are some examples of pairs of classes that you can create that will have a "has many / belongs to" relationship.
0 commit comments