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
@@ -24,7 +28,11 @@ Curiously Recurring Template Pattern (CRTP) is used to achieve a form of static
24
28
25
29
Real-world example
26
30
27
-
> For a mixed martial arts promotion that is planning an event, ensuring that the fights are organized between athletes of the same weight class is crucial. This prevents mismatches between fighters of significantly different sizes, such as a heavyweight facing off against a bantamweight.
31
+
> Consider a scenario where a library system manages various types of media: books, DVDs, and magazines. Each media type has specific attributes and behaviors, but they all share common functionality like borrowing and returning.
32
+
>
33
+
> Using the Curiously Recurring Template Pattern (CRTP), you can create a base template class `MediaItem` that includes these common methods. Each specific media type (e.g., `Book`, `DVD`, `Magazine`) would then inherit from `MediaItem` using itself as a template parameter. This allows each media type to customize the common functionality without the overhead of virtual methods.
34
+
>
35
+
> For example, `Book` would inherit from `MediaItem<Book>`, allowing the library system to use polymorphic behavior at compile-time, ensuring that each media type implements the necessary methods efficiently. This approach provides the benefits of polymorphism and code reuse while maintaining high performance and type safety.
28
36
29
37
In plain words
30
38
@@ -36,7 +44,9 @@ Wikipedia says
36
44
37
45
**Programmatic example**
38
46
39
-
Let's define the generic interface Fighter.
47
+
For a mixed martial arts promotion that is planning an event, ensuring that the fights are organized between athletes of the same weight class is crucial. This prevents mismatches between fighters of significantly different sizes, such as a heavyweight facing off against a bantamweight.
48
+
49
+
Let's define the generic interface `Fighter`.
40
50
41
51
```java
42
52
publicinterfaceFighter<T> {
@@ -46,93 +56,64 @@ public interface Fighter<T> {
46
56
}
47
57
```
48
58
49
-
The MMAFighter class is used to instantiate fighters distinguished by their weight class.
59
+
The `MMAFighter` class is used to instantiate fighters distinguished by their weight class.
MmaBantamweightFighter fighter1 =newMmaBantamweightFighter("Joe", "Johnson", "The Geek", "Muay Thai");
102
+
MmaBantamweightFighter fighter2 =newMmaBantamweightFighter("Ed", "Edwards", "The Problem Solver", "Judo");
103
+
fighter1.fight(fighter2);
129
104
130
-
fighter1.fight(fighter3); // This will raise a compilation error
105
+
MmaHeavyweightFighter fighter3 =newMmaHeavyweightFighter("Dave", "Davidson", "The Bug Smasher", "Kickboxing");
106
+
MmaHeavyweightFighter fighter4 =newMmaHeavyweightFighter("Jack", "Jackson", "The Pragmatic", "Brazilian Jiu-Jitsu");
107
+
fighter3.fight(fighter4);
108
+
}
131
109
```
132
110
133
-
## Class diagram
111
+
Program output:
134
112
135
-

113
+
```
114
+
08:42:34.048 [main] INFO crtp.MmaFighter -- MmaFighter(name=Joe, surname=Johnson, nickName=The Geek, speciality=Muay Thai) is going to fight against MmaFighter(name=Ed, surname=Edwards, nickName=The Problem Solver, speciality=Judo)
115
+
08:42:34.054 [main] INFO crtp.MmaFighter -- MmaFighter(name=Dave, surname=Davidson, nickName=The Bug Smasher, speciality=Kickboxing) is going to fight against MmaFighter(name=Jack, surname=Jackson, nickName=The Pragmatic, speciality=Brazilian Jiu-Jitsu)
* Yogesh Umesh Vaity's answer to [What does "Recursive type bound" in Generics mean?](https://stackoverflow.com/questions/7385949/what-does-recursive-type-bound-in-generics-mean)
129
+
*[Curiously Recurring Template Pattern in Java (The NuaH Blog)](https://nuah.livejournal.com/328187.html)
150
130
151
131
## Known uses
152
132
153
133
* Implementing compile-time polymorphic interfaces in template libraries.
154
134
* Enhancing code reuse in libraries where performance is critical, like in mathematical computations, embedded systems, and real-time processing applications.
0 commit comments