Chapter 13. Subtyping with Plain JS

Table of Contents
1. Subtyping with Constructor-Based Classes
2. Case Study 1: Eliminating a Class Hierarchy
2.1. Make the JS class model
2.2. New issues
2.3. Code the model classes of the JS class model
2.3.1. Summary
2.3.2. Code the enumeration type BookCategoryEL
2.3.3. Code the model class Book
2.4. Write the View and Controller Code
2.4.1. Summary
2.4.2. Add a segment information column in Retrieve/List All
2.4.3. Add a category selection field in Create and Update
3. Case Study 2: Implementing a Class Hierarchy
3.1. Make a JS class model
3.2. Make a JS entity table model
3.3. New issues
3.4. Code the model classes of the JS class model
3.4.1. Defining subtype relationships
3.4.2. Loading the instances of the root class Person
3.4.3. Saving the supertable when saving a subtable
4. Quiz Questions
4.1. Question 1: Constructor-Based Inheritance Pattern
4.2. Question 2: Check Method for Segment Property

In this chapter, we first explain the general approach to constructor-based subtyping in JavaScript before presenting two case studies based on fragments of the information model of our running example, the Public Library app, shown above.

In the first case study, we consider the single-level class hierarchy with root Book shown in Figure 12.1, which is an incomplete disjoint rigid segmentation. We use the Class Hierarchy Merge design pattern for re-factoring this simple class hierarchy to a single class that can be mapped to a persistent database table.

In the second case study, we consider the multi-level class hierarchy consisting of the Person roles Employee, Manager and Author, shown in Figure 12.8. The segmentation of Person into Employee and Author does not have any constraints, which means that it is incomplete, overlapping (non-disjoint) and non-rigid.

We use the Class Hierarchy Merge design pattern for re-factoring the simple Manager-is-Employee sub-hierarchy, and the Joined Tables Inheritance approach for mapping the Employee-and-Author-is-a-Person class hierarchy to a set of three database tables that are related with each other via foreign key dependencies.

In both case studies we show

  1. how to derive a JS class model, and a corresponding entity table model, from the information design model,

  2. how to code the JS class model in the form of JS model classes,

  3. how to write the view and controller code based on the model code.