Marcy Lab School Docs
  • Welcome
  • Student Guidelines & Policies
    • Student Handbook
    • AI Policy
    • Academic Calendar
  • Environment Setup
    • Local Environment Setup - Mac
    • Local Environment Setup - Windows
    • GitHub Setup
    • Postgres Setup
  • How-Tos
    • How To Code at Marcy: Code Style Guide
    • How to Do Short Response and Coding Assignments
    • How to Debug
    • How to PEDAC
    • How to Create A GitHub Organization and Scrumboard
    • How to Create Projects with Vite
    • How to Deploy on GitHub Pages
    • How to Deploy on Render
    • How to Test your API with Postman
  • Mod 0 - Command Line Interfaces, Git, and GitHub
    • Overview
    • 1. Command Line Interfaces
    • 2. Git & GitHub
    • 3. Git Pulling & Merging
    • 4. Git Branching & PRs
  • Mod 1 - JavaScript Fundamentals
    • Overview
    • 1. Intro to Programming
    • 2. Errors
    • 3. Node & Node Modules
    • 4. Variables, Functions & String Methods
    • 5. Control Flow, typeof, and Math
    • 6. Loops
    • 7. Arrays
    • 8. Objects
    • 9. Higher Order Functions: Callbacks
    • 10. Higher Order Functions: Array Methods
    • 11. Regex
  • Mod 2 - HTML, CSS & the DOM
    • Overview
    • 1. HTML
    • 2. CSS
    • 3. Accessibility (a11y)
    • 4. The Document Object Model (DOM) API
    • 5. Events
    • 6. Forms
    • 7. The Box Model and Positioning
    • 8. Flexbox
    • 9. Grid & Media Queries
    • 10. ESModules
    • 11. Vite
    • 12. LocalStorage
  • Mod 3 - Async & APIs
    • Overview
    • 1. Promises
    • 2. Fetch
    • 3. Building a Fetching App
    • 4. Async & Await
    • 5. A Generic Fetch Handler
  • Mod 4 - Project Week!
    • Important How Tos and Guides
      • How to Create a GitHub Organization and Scrum Board
      • How To Start a Project with Vite
      • How To Deploy a Project with GitHub Pages
    • Project Week Overview
    • Agile Methodologies
    • Deliverables & Milestones
    • Technical Requirements Checklist
    • Free API List
    • Collaborative GitHub
  • Mod 5 - Object-Oriented Programming
    • Overview
    • 1. Intro to OOP, Encapsulation, Factory Functions, and Closure
    • 2. Classes
    • 3. Private & Static
    • 4. UML Diagrams & Has Many/Belongs To Relationships
    • 5. Challenge: Implementing Has Many/Belongs To
    • 6. Inheritance
    • 7. Polymorphism
    • 8. Review and Practice
    • MDN: Object Prototypes
  • Mod 6 - Data Structures & Algorithms
    • Overview
    • Important How Tos and Guides
      • How to Debug
      • How to PEDAC
    • 1. Nodes & Linked Lists
    • 2. Singly & Doubly Linked Lists
    • 3. Stacks & Queues
    • 4. Recursion
    • 5. Trees
  • Mod 7 - React
    • Overview
    • Important How Tos and Guides
      • How to Create Projects with Vite
      • How to Deploy on GitHub Pages
    • 1. Intro to React
    • 2. Events, State, and Forms
    • 3. Fetching with useEffect
    • 4. React Router
    • 5. Building a Flashcards App
    • 6. React Context
    • 7. Global Context Pattern
  • Mod 8 - Backend
    • Overview
    • Important How Tos and Guides
      • How to Deploy on Render
      • How to Test your API with Postman
      • Postgres Setup
    • 1. Intro to Express
    • 2. Building a Static Web Server with Middleware
    • 3. Securing API Keys and Environment Variables
    • 4. RESTful CRUD API
    • 5. Model-View-Controller Architecture
    • 6. SQL and Databases
    • 7. JOIN (Association) SQL Queries
    • 8. Knex
    • 9. Your First Fullstack App!
    • 10. Migrations & Seeds
    • 11. Schema Design & Normalization
    • 12. Hashing Passwords with Bcrypt
    • 13. React Express Auth Template Overview
  • Mod 9 - Civic Tech Hackathon
    • Overview
    • Rubric
  • Mod 10 - Capstone
    • Overview
Powered by GitBook
On this page
  • Intro: Class Diagrams
  • Practice: The Book Class
  • Has Many / Belongs To Relationships Between Classes
  • Practice: The Library Class
  • Challenge
  1. Mod 5 - Object-Oriented Programming

4. UML Diagrams & Has Many/Belongs To Relationships

Previous3. Private & StaticNext5. Challenge: Implementing Has Many/Belongs To

Last updated 3 months ago

Follow along with code examples !

Table of Contents:

Intro: Class Diagrams

When building an application, the first step is always to make a plan.

  • We plan the features of the application with user stories

  • We plan the user interface with a wireframe

Now that we're preparing to build more complex applications, we need to start thinking about how we plan our data. This plan is often called the data architecture and can be represented with a Class Diagram.

UML stands for Unified Modeling Language and it defines a way of describing classes and their relationships.

Practice: The Book Class

TODO: Take a moment and create a UML diagram for the Book class below:

class Book {
  static #allBooks = []

  constructor(title, author, genre) {
    this.title = title;
    this.author = author;
    this.genre = genre;
    this.id = getId();

    Book.#allBooks.push(this);
  }

  static list() {
    return [...Book.#allBooks];
  }

  static find(id) {
    return Book.#allBooks.find((book) => book.id === id);
  }
}

Has Many / Belongs To Relationships Between Classes

Often, classes will interact with each other. The way in which they interact defines what kind of relationship exists between the two classes.

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.

We can turn our diagrams from simple class diagrams to Entity Relationship Diagrams (ERDs) by connecting them.

Practice: The Library Class

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.

class Library {
  #books = [];                // Private Instance Property
  static #allLibraries = []   // Private Class Property

  constructor(name, address) {
    this.name = name;
    this.address = address;
    this.id = getId();

    Library.#allLibraries.push(this);
  }

  // Library Instance Methods
  addBook(title, author, genre) {
    // When adding a book to the Library, a new Book instance is created. 
    const addedBook = new Book(title, author, genre);
    // The Book is added to this library's collection, forming the "has many/belongs to" relationship
    this.#books.push(addedBook);
    return addedBook;
  }
  listBooks() {
    return [...this.#books];
  }
  removeBook(id) {
    const matchingBookIndex = this.#books.findIndex((book) => book.id === id);
    this.#books.splice(matchingBookIndex, 1);
  }

  // Library Class Methods
  static list() {
    return [...Library.#allLibraries];
  }
  static find(id) {
    return Library.#allLibraries.find((library) => library.id === id);
  }
}
Q: What is the relationship between a Library and a Book class?

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

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

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

Example: Library and Books ERD

Challenge

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

  • Doctor and Appointment

  • Playlist and Song

  • Group and User

  • Class and Student

With a partner:

  1. Create the class diagram for each class along with the relationship arrow between them.

  2. Then, implement the classes in JavaScript.

Attempt to have a combination of private and public properties / methods, and static and instance properties / methods.

Check out the library-book-example.js file for an example.

UML Diagrams can be created using a tool like or they can simply be drawn using pen and paper.

https://draw.io
here
Intro: Class Diagrams
Practice: The Book Class
Has Many / Belongs To Relationships Between Classes
Practice: The Library Class
Challenge