Skip to content

Commit 5fa526c

Browse files
authored
Merge branch 'master' into Taha
2 parents 431dac5 + ac53960 commit 5fa526c

File tree

15 files changed

+777
-64
lines changed

15 files changed

+777
-64
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,6 +3312,15 @@
33123312
"contributions": [
33133313
"translation"
33143314
]
3315+
},
3316+
{
3317+
"login": "HabibaMekay",
3318+
"name": "HabibaMekay",
3319+
"avatar_url": "https://avatars.githubusercontent.com/u/133516736?v=4",
3320+
"profile": "https://github.com/HabibaMekay",
3321+
"contributions": [
3322+
"code"
3323+
]
33153324
}
33163325
],
33173326
"contributorsPerLine": 6,

.github/stale.yml

Lines changed: 0 additions & 61 deletions
This file was deleted.

.github/workflows/stale.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: 'Comment on stale issues and PRs'
2+
on:
3+
schedule:
4+
- cron: '30 1 * * *'
5+
6+
jobs:
7+
stale:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/stale@v9
11+
with:
12+
stale-issue-message: 'This issue is stale because it has been open 60 days with no activity.'
13+
stale-pr-message: 'This PR is stale because it has been open 60 days with no activity.'
14+
close-issue-message: 'This issue was closed because it has been stalled for too long with no activity.'
15+
close-pr-message: 'This PR was closed because it has been stalled for too long with no activity.'
16+
days-before-issue-stale: 60
17+
days-before-pr-stale: 60
18+
days-before-issue-close: -1
19+
days-before-pr-close: -1

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
77
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
88
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
9-
[![All Contributors](https://img.shields.io/badge/all_contributors-362-orange.svg?style=flat-square)](#contributors-)
9+
[![All Contributors](https://img.shields.io/badge/all_contributors-364-orange.svg?style=flat-square)](#contributors-)
1010
<!-- ALL-CONTRIBUTORS-BADGE:END -->
1111

1212
<br/>
@@ -544,6 +544,7 @@ This project is licensed under the terms of the MIT license.
544544
<td align="center" valign="top" width="16.66%"><a href="https://github.com/SalmaAzeem"><img src="https://avatars.githubusercontent.com/u/121863224?v=4?s=100" width="100px;" alt="Salma"/><br /><sub><b>Salma</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=SalmaAzeem" title="Code">💻</a></td>
545545
<td align="center" valign="top" width="16.66%"><a href="https://codemaverick-143.github.io/My-Portfolio/"><img src="https://avatars.githubusercontent.com/u/182847716?v=4?s=100" width="100px;" alt="Arpit Sarang"/><br /><sub><b>Arpit Sarang</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=CodeMaverick-143" title="Code">💻</a></td>
546546
<td align="center" valign="top" width="16.66%"><a href="https://github.com/mayatarek"><img src="https://avatars.githubusercontent.com/u/111644421?v=4?s=100" width="100px;" alt="Maya"/><br /><sub><b>Maya</b></sub></a><br /><a href="#translation-mayatarek" title="Translation">🌍</a></td>
547+
<td align="center" valign="top" width="16.66%"><a href="https://github.com/HabibaMekay"><img src="https://avatars.githubusercontent.com/u/133516736?v=4?s=100" width="100px;" alt="HabibaMekay"/><br /><sub><b>HabibaMekay</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=HabibaMekay" title="Code">💻</a></td>
547548
</tr>
548549
</tbody>
549550
</table>

hexagonal-architecture/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<dependency>
4747
<groupId>de.flapdoodle.embed</groupId>
4848
<artifactId>de.flapdoodle.embed.mongo</artifactId>
49-
<version>4.18.0</version>
49+
<version>4.18.1</version>
5050
<scope>test</scope>
5151
</dependency>
5252
<dependency>

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@
218218
<module>function-composition</module>
219219
<module>microservices-distributed-tracing</module>
220220
<module>microservices-idempotent-consumer</module>
221-
<module>money</module>
221+
<module>money</module>
222+
<module>table-inheritance</module>
222223
</modules>
223224
<repositories>
224225
<repository>

table-inheritance/README.md

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
---
2+
title: "Table Inheritance Pattern in Java: Modeling Hierarchical Data in Relational Databases"
3+
shortTitle: Table Inheritance
4+
description: "Explore the Table Inheritance pattern in Java with real-world examples, database schema, and tutorials. Learn how to model class hierarchies elegantly in relational databases."
5+
category: Data Access Pattern, Structural Pattern
6+
language: en
7+
tag:
8+
- Decoupling
9+
- Inheritance
10+
- Polymorphism
11+
- Object Mapping
12+
- Persistence
13+
- Data Transformation
14+
---
15+
16+
## Also Known As
17+
- Class Table Inheritance
18+
---
19+
20+
## Intent of Table Inheritance Pattern
21+
The Table Inheritance pattern models a class hierarchy in a relational database by creating
22+
separate tables for each class in the hierarchy. These tables share a common primary key, which in
23+
subclass tables also serves as a foreign key referencing the primary key of the base class table.
24+
This linkage maintains relationships and effectively represents the inheritance structure. This pattern
25+
enables the organization of complex data models, particularly when subclasses have unique properties
26+
that must be stored in distinct tables.
27+
28+
---
29+
30+
## Detailed Explanation of Table Inheritance Pattern with Real-World Examples
31+
32+
### Real-World Example
33+
Consider a **Vehicle Management System** with a `Vehicle` superclass and subclasses like `Car` and `Truck`.
34+
35+
- The **Vehicle Table** stores attributes common to all vehicles, such as `make`, `model`, and `year`. Its primary key (`id`) uniquely identifies each vehicle.
36+
- The **Car Table** and **Truck Table** store attributes specific to their respective types, such as `numberOfDoors` for cars and `payloadCapacity` for trucks.
37+
- The `id` column in the **Car Table** and **Truck Table** serves as both the primary key for those tables and a foreign key referencing the `id` in the **Vehicle Table**.
38+
39+
This setup ensures each subclass entry corresponds to a base class entry, maintaining the inheritance relationship while keeping subclass-specific data in their own tables.
40+
41+
### In Plain Words
42+
In table inheritance, each class in the hierarchy is represented by a separate table, which
43+
allows for a clear distinction between shared attributes (stored in the base class table) and
44+
specific attributes (stored in subclass tables).
45+
46+
### Martin Fowler Says
47+
48+
Relational databases don't support inheritance, which creates a mismatch when mapping objects.
49+
To fix this, Table Inheritance uses a separate table for each class in the hierarchy while maintaining
50+
relationships through foreign keys, making it easier to link the classes together in the database.
51+
52+
For more detailed information, refer to Martin Fowler's article on [Class Table Inheritance](https://martinfowler.com/eaaCatalog/classTableInheritance.html).
53+
54+
55+
## Programmatic Example of Table Inheritance Pattern in Java
56+
57+
58+
The `Vehicle` class will be the superclass, and we will have `Car` and `Truck` as subclasses that extend
59+
`Vehicle`. The `Vehicle` class will store common attributes, while `Car` and `Truck` will store
60+
attributes specific to those subclasses.
61+
62+
### Key Aspects of the Pattern:
63+
64+
1. **Superclass (`Vehicle`)**:
65+
The `Vehicle` class stores attributes shared by all vehicle types, such as:
66+
- `make`: The manufacturer of the vehicle.
67+
- `model`: The model of the vehicle.
68+
- `year`: The year the vehicle was manufactured.
69+
- `id`: A unique identifier for the vehicle.
70+
71+
These attributes are stored in the **`Vehicle` table** in the database.
72+
73+
2. **Subclass (`Car` and `Truck`)**:
74+
Each subclass (`Car` and `Truck`) stores attributes specific to that vehicle type:
75+
- `Car`: Has an additional attribute `numberOfDoors` representing the number of doors the car has.
76+
- `Truck`: Has an additional attribute `payloadCapacity` representing the payload capacity of the truck.
77+
78+
These subclass-specific attributes are stored in the **`Car` and `Truck` tables**.
79+
80+
3. **Foreign Key Relationship**:
81+
Each subclass (`Car` and `Truck`) contains the `id` field which acts as a **foreign key** that
82+
references the primary key (`id`) of the superclass (`Vehicle`). This foreign key ensures the
83+
relationship between the common attributes in the `Vehicle` table and the specific attributes in the
84+
subclass tables (`Car` and `Truck`).
85+
86+
87+
```java
88+
/**
89+
* Superclass
90+
* Represents a generic vehicle with basic attributes like make, model, year, and ID.
91+
*/
92+
public class Vehicle {
93+
private String make;
94+
private String model;
95+
private int year;
96+
private int id;
97+
98+
// Constructor, getters, and setters...
99+
}
100+
101+
/**
102+
* Represents a car, which is a subclass of Vehicle.
103+
*/
104+
public class Car extends Vehicle {
105+
private int numberOfDoors;
106+
107+
// Constructor, getters, and setters...
108+
}
109+
110+
/**
111+
* Represents a truck, which is a subclass of Vehicle.
112+
*/
113+
public class Truck extends Vehicle {
114+
private int payloadCapacity;
115+
116+
// Constructor, getters, and setters...
117+
}
118+
```
119+
120+
121+
122+
## Table Inheritance Pattern Class Diagram
123+
124+
125+
<img src="etc/class-diagram.png" width="400" height="500" />
126+
127+
128+
129+
130+
131+
132+
## Table Inheritance Pattern Database Schema
133+
134+
### Vehicle Table
135+
| Column | Description |
136+
|--------|-------------------------------------|
137+
| id | Primary key |
138+
| make | The make of the vehicle |
139+
| model | The model of the vehicle |
140+
| year | The manufacturing year of the vehicle |
141+
142+
### Car Table
143+
| Column | Description |
144+
|------------------|-------------------------------------|
145+
| id | Foreign key referencing `Vehicle(id)` |
146+
| numberOfDoors | Number of doors in the car |
147+
148+
### Truck Table
149+
| Column | Description |
150+
|-------------------|-------------------------------------|
151+
| id | Foreign key referencing `Vehicle(id)` |
152+
| payloadCapacity | Payload capacity of the truck |
153+
154+
---
155+
156+
## When to Use the Table Inheritance Pattern in Java
157+
158+
- When your application requires a clear mapping of an object-oriented class hierarchy to relational tables.
159+
- When subclasses have unique attributes that do not fit into a single base table.
160+
- When scalability and normalization of data are important considerations.
161+
- When you need to separate concerns and organize data in a way that each subclass has its own
162+
table but maintains relationships with the superclass.
163+
164+
## Table Inheritance Pattern Java Tutorials
165+
166+
- [Software Patterns Lexicon: Class Table Inheritance](https://softwarepatternslexicon.com/patterns-sql/4/4/2/)
167+
- [Martin Fowler: Class Table Inheritance](http://thierryroussel.free.fr/java/books/martinfowler/www.martinfowler.com/isa/classTableInheritance.html)
168+
169+
---
170+
171+
## Real-World Applications of Table Inheritance Pattern in Java
172+
173+
- **Vehicle Management System**: Used to store different types of vehicles like Car and Truck in separate tables but maintain a relationship through a common superclass `Vehicle`.
174+
- **E-Commerce Platforms**: Where different product types, such as Clothing, Electronics, and Furniture, are stored in separate tables with shared attributes in a superclass `Product`.
175+
176+
## Benefits and Trade-offs of Table Inheritance Pattern
177+
178+
### Benefits
179+
180+
- **Clear Structure**: Each class has its own table, making the data model easier to maintain and understand.
181+
- **Scalability**: Each subclass can be extended independently without affecting the other tables, making the system more scalable.
182+
- **Data Normalization**: Helps avoid data redundancy and keeps the schema normalized.
183+
184+
### Trade-offs
185+
186+
- **Multiple Joins**: Retrieving data that spans multiple subclasses may require joining multiple tables, which could lead to performance issues.
187+
- **Increased Complexity**: Managing relationships between tables and maintaining integrity can become more complex.
188+
- **Potential for Sparse Tables**: Subclasses with fewer attributes may end up with tables that have many null fields.
189+
190+
## Related Java Design Patterns
191+
192+
- **Single Table Inheritance** – A strategy where a single table is used to store all classes in an
193+
inheritance hierarchy. It stores all attributes of the class and its subclasses in one table.
194+
- **Singleton Pattern** – Used when a class needs to have only one instance.
195+
196+
197+
## References and Credits
198+
199+
- **Martin Fowler** - [*Patterns of Enterprise Application Architecture*](https://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420)
200+
- **Java Persistence with Hibernate** - [Link to book](https://www.amazon.com/Java-Persistence-Hibernate-Christian-Bauer/dp/193239469X)
201+
- **Object-Relational Mapping on Wikipedia** - [Link to article](https://en.wikipedia.org/wiki/Object-relational_mapping)

table-inheritance/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.iluwatar</groupId>
8+
<artifactId>java-design-patterns</artifactId>
9+
<version>1.26.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>table-inheritance</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.junit.jupiter</groupId>
17+
<artifactId>junit-jupiter-engine</artifactId>
18+
<version>5.7.0</version>
19+
<scope>test</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.projectlombok</groupId>
23+
<artifactId>lombok</artifactId>
24+
<version>1.18.24</version>
25+
<scope>provided</scope>
26+
</dependency>
27+
</dependencies>
28+
29+
30+
31+
</project>

0 commit comments

Comments
 (0)