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
Copy file name to clipboardExpand all lines: cqrs/README.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ tag:
10
10
11
11
## Intent
12
12
13
-
CQRS aims to segregate the operations that modify the state of an application (commands) from the operations that read the state (queries). This separation allows for more flexible and optimized designs, especially in complex systems.
13
+
Command Query Responsibility Segregation (CQRS) aims to segregate the operations that modify the state of an application (commands) from the operations that read the state (queries). This separation allows for more flexible and optimized designs, especially in complex systems.
Copy file name to clipboardExpand all lines: crtp/README.md
+101-60
Original file line number
Diff line number
Diff line change
@@ -1,114 +1,132 @@
1
1
---
2
-
title: Curiously Recurring Template Pattern
2
+
title: CRTP
3
3
language: en
4
4
category: Structural
5
5
tag:
6
-
- Extensibility
7
-
- Instantiation
6
+
- Extensibility
7
+
- Idiom
8
+
- Instantiation
8
9
---
9
10
10
-
## Name / classification
11
-
12
-
Curiously Recurring Template Pattern
13
-
14
11
## Also known as
15
12
16
-
Recursive Type Bound, Recursive Generic
13
+
* Recursive Type Bound
14
+
* Recursive Generic
15
+
* Static Polymorphism
16
+
* Mixin Inheritance
17
17
18
18
## Intent
19
19
20
-
Allow derived components to inherit certain functionalities from a base component that are compatible with the derived type.
20
+
Curiously Recurring Template Pattern (CRTP) is used to achieve a form of static polymorphism by having a class template derive from a template instantiation of its own class, allowing method overriding and polymorphic behavior at compile time rather than at runtime.
21
21
22
22
## Explanation
23
23
24
24
Real-world example
25
25
26
-
> For a mixed martial arts promotion that is planning an event, ensuring that the fights are organized between athletes
27
-
> of the same weight class is crucial. This prevents mismatches between fighters of significantly different sizes, such
28
-
> as a heavyweight facing off against a bantamweight.
26
+
> 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.
29
27
30
28
In plain words
31
29
32
30
> Make certain methods within a type to accept arguments specific to its subtypes.
33
31
34
32
Wikipedia says
35
33
36
-
> The curiously recurring template pattern (CRTP) is an idiom, originally in C++, in which a class X
37
-
> derives from a class template instantiation using X itself as a template argument.
34
+
> The curiously recurring template pattern (CRTP) is an idiom, originally in C++, in which a class X derives from a class template instantiation using X itself as a template argument.
38
35
39
36
**Programmatic example**
40
37
41
-
Let's define the generic interface Fighter
38
+
Let's define the generic interface Fighter.
42
39
43
40
```java
44
41
publicinterfaceFighter<T> {
45
42
46
-
voidfight(Tt);
43
+
voidfight(Tt);
47
44
48
45
}
49
46
```
50
47
51
-
The MMAFighter class is used to instantiate fighters distinguished by their weight class
48
+
The MMAFighter class is used to instantiate fighters distinguished by their weight class.
fighter1.fight(fighter3); // This will raise a compilation error
129
+
fighter1.fight(fighter3); // This will raise a compilation error
112
130
```
113
131
114
132
## Class diagram
@@ -117,22 +135,45 @@ fighter1.fight(fighter3); // This will raise a compilation error
117
135
118
136
## Applicability
119
137
120
-
Use the CuriouslyRecurringTemplatePattern when
121
-
122
-
*You have type conflicts when chaining methods in an object hierarchy
123
-
* You want to use a parameterized classmethod that can accept subclasses of the classas arguments, allowing it to be applied to objects that inherit from the class
138
+
* When you need to extend the functionality of a class through inheritance but prefer compile-time polymorphism to runtime polymorphism for efficiency reasons.
139
+
* When you want to avoid the overhead of virtual functions but still achieve polymorphic behavior.
140
+
* In template metaprogramming to provide implementations of functions or policies that can be selected at compile time.
141
+
* You have type conflicts when chaining methods in an object hierarchy.
142
+
* You want to use a parameterized class method that can accept subclasses of the class as arguments, allowing it to be applied to objects that inherit from the class.
124
143
* You want certain methods to work only with instances of the same type, such as for achieving mutual comparability.
* Yogesh Umesh Vaity answer to [What does "Recursive type bound" in Generics mean?](https://stackoverflow.com/questions/7385949/what-does-recursive-type-bound-in-generics-mean)
148
+
* 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)
130
149
131
150
## Known uses
132
151
152
+
* Implementing compile-time polymorphic interfaces in template libraries.
153
+
* Enhancing code reuse in libraries where performance is critical, like in mathematical computations, embedded systems, and real-time processing applications.
* Elimination of virtual function call overhead, enhancing performance.
161
+
* Safe reuse of the base class code without the risks associated with multiple inheritances.
162
+
* Greater flexibility and extensibility in compile-time polymorphism scenarios.
163
+
164
+
Trade-offs:
165
+
166
+
* Increased complexity in understanding and debugging due to the interplay of templates and inheritance.
167
+
* Can lead to code bloat because each instantiation of a template results in a new class.
168
+
* Less flexibility compared to runtime polymorphism as the behavior must be determined entirely at compile time.
169
+
170
+
## Related Patterns
171
+
172
+
*[Factory Method](https://java-design-patterns.com/patterns/factory-method/): Can be used in conjunction with CRTP to instantiate derived classes without knowing their specific types.
173
+
*[Strategy](https://java-design-patterns.com/patterns/strategy/): CRTP can implement compile-time strategy selection.
174
+
*[Template Method](https://java-design-patterns.com/patterns/template-method/): Similar in structure but differs in that CRTP achieves behavior variation through compile-time polymorphism.
0 commit comments