Skip to content

Commit 41a1521

Browse files
clarifying has many belongs to lesson
1 parent 4acdcdd commit 41a1521

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
* [Intro to OOP, Encapsulation, Factory Functions, and Closure](mod-5-oop/1-encapsulation-factories-and-closure.md)
7575
* [Classes](mod-5-oop/2-classes.md)
7676
* [Private & Static](mod-5-oop/3-private-properties-static-methods.md)
77-
* [Has Many/Belongs To](mod-5-oop/5-has-many-belongs-to.md)
77+
* [UML Diagrams & Has Many/Belongs To Relationships](mod-5-oop/5-has-many-belongs-to.md)
7878
* [Challenge: Implementing Has Many/Belongs To](mod-5-oop/6-has-many-belongs-to-frontend.md)
7979
* [Inheritance](mod-5-oop/7-inheritance.md)
8080
* [Polymorphism](mod-5-oop/8-polymorphism.md)

mod-5-oop/5-has-many-belongs-to.md

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
# Has Many/Belongs To
1+
# UML Diagrams and Has Many/Belongs To Relationships
22

33
{% hint style="info" %}
44
Follow along with code examples [here](https://github.com/The-Marcy-Lab-School/5-1-0-has-many-belongs-to)!
55
{% endhint %}
66

77
**Table of Contents:**
88
- [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)
1213

1314
## Intro: Class Diagrams
1415

@@ -23,7 +24,7 @@ UML stands for **U**nified **M**odeling **L**anguage and it defines a way of des
2324

2425
![](img/uml-diagrams.svg)
2526

26-
## Practice
27+
### Practice: The Book Class
2728

2829
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.
2930

@@ -52,7 +53,19 @@ class Book {
5253
}
5354
```
5455

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+
![](img/relationships.png)
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.
5669

5770
```js
5871
class Library {
@@ -69,15 +82,18 @@ class Library {
6982

7083
// Library Instance Methods
7184
addBook(title, author, genre) {
85+
// When adding a book to the Library, a new Book instance is created.
7286
const addedBook = new Book(title, author, genre);
87+
// The Book is added to this library's collection, forming the "has many/belongs to" relationship
7388
this.#books.push(addedBook);
7489
return addedBook;
7590
}
7691
listBooks() {
7792
return [...this.#books];
7893
}
7994
removeBook(id) {
80-
this.#books.splice(this.#books.findIndex((book) => book.id === id), 1);
95+
const matchingBookIndex = this.#books.findIndex((book) => book.id === id);
96+
this.#books.splice(matchingBookIndex, 1);
8197
}
8298

8399
// Library Class Methods
@@ -92,40 +108,16 @@ class Library {
92108

93109
<details>
94110

95-
<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.
98-
99-
<img src="img/book-libarary-class-diagram.png" alt="Alt text" data-size="original">
100-
101-
</details>
102-
103-
104-
105-
## Entity Relationships
106-
107-
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-
![](img/relationships.png)
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>
119112

120113
A library has many books. A book belongs to a Library
121114

122-
</details>
123-
115+
Later this week, we'll learn how to implement an "Is A" relationship with the `extends` keyword.
124116

117+
</details>
125118

126-
Later this week, we'll learn how to implement an "Is A" relationship with the `extends` keyword.
127119

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
129121

130122
<details>
131123

@@ -137,7 +129,7 @@ Later this week, we'll learn how to implement an "Is A" relationship with the `e
137129

138130

139131

140-
## Make some has many / belongs to class relationships
132+
## Challenge
141133

142134
Below are some examples of pairs of classes that you can create that will have a "has many / belongs to" relationship.
143135

0 commit comments

Comments
 (0)