From 85823b47325c5cb45e3c657d022b5ff13a6863e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Sat, 6 Apr 2024 19:56:37 +0300 Subject: [PATCH] docs: improve crtp documentation --- cqrs/README.md | 2 +- .../main/java/com/iluwatar/cqrs/app/App.java | 2 +- crtp/README.md | 155 +++++++++++------- 3 files changed, 100 insertions(+), 59 deletions(-) diff --git a/cqrs/README.md b/cqrs/README.md index e4af36bc3..cd389cef8 100644 --- a/cqrs/README.md +++ b/cqrs/README.md @@ -10,7 +10,7 @@ tag: ## Intent -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. +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. ## Explanation diff --git a/cqrs/src/main/java/com/iluwatar/cqrs/app/App.java b/cqrs/src/main/java/com/iluwatar/cqrs/app/App.java index 6e73318af..4552ede3d 100644 --- a/cqrs/src/main/java/com/iluwatar/cqrs/app/App.java +++ b/cqrs/src/main/java/com/iluwatar/cqrs/app/App.java @@ -32,7 +32,7 @@ import lombok.extern.slf4j.Slf4j; /** * CQRS : Command Query Responsibility Segregation. A pattern used to separate query services from - * commands or writes services. The pattern is very simple but it has many consequences. For + * commands or writes services. The pattern is very simple, but it has many consequences. For * example, it can be used to tackle down a complex domain, or to use other architectures that were * hard to implement with the classical way. * diff --git a/crtp/README.md b/crtp/README.md index 67627f44d..cfb438c06 100644 --- a/crtp/README.md +++ b/crtp/README.md @@ -1,31 +1,29 @@ --- -title: Curiously Recurring Template Pattern +title: CRTP language: en category: Structural tag: -- Extensibility -- Instantiation + - Extensibility + - Idiom + - Instantiation --- -## Name / classification - -Curiously Recurring Template Pattern - ## Also known as -Recursive Type Bound, Recursive Generic +* Recursive Type Bound +* Recursive Generic +* Static Polymorphism +* Mixin Inheritance ## Intent -Allow derived components to inherit certain functionalities from a base component that are compatible with the derived type. +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. ## Explanation Real-world example -> 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. +> 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. In plain words @@ -33,82 +31,102 @@ In plain words Wikipedia says -> 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. +> 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. **Programmatic example** -Let's define the generic interface Fighter +Let's define the generic interface Fighter. ```java public interface Fighter { - void fight(T t); + void fight(T t); } ``` -The MMAFighter class is used to instantiate fighters distinguished by their weight class +The MMAFighter class is used to instantiate fighters distinguished by their weight class. ```java public class MmaFighter> implements Fighter { - private final String name; - private final String surname; - private final String nickName; - private final String speciality; + private final String + name; + private final String + surname; + private final String + nickName; + private final String + speciality; - public MmaFighter(String name, String surname, String nickName, String speciality) { - this.name = name; - this.surname = surname; - this.nickName = nickName; - this.speciality = speciality; - } + public MmaFighter( + String name, + String surname, + String nickName, + String speciality) { + this.name = + name; + this.surname = + surname; + this.nickName = + nickName; + this.speciality = + speciality; + } - @Override - public void fight(T opponent) { - LOGGER.info("{} is going to fight against {}", this, opponent); - } + @Override + public void fight( + T opponent) { + LOGGER.info( + "{} is going to fight against {}", + this, + opponent); + } - @Override - public String toString() { - return name + " \"" + nickName + "\" " + surname; - } + @Override + public String toString() { + return + name + + " \"" + + nickName + + "\" " + + surname; + } +} ``` -The followings are some subtypes of MmaFighter +The followings are some subtypes of MmaFighter. ```java class MmaBantamweightFighter extends MmaFighter { - public MmaBantamweightFighter(String name, String surname, String nickName, String speciality) { - super(name, surname, nickName, speciality); - } + public MmaBantamweightFighter(String name, String surname, String nickName, String speciality) { + super(name, surname, nickName, speciality); + } } public class MmaHeavyweightFighter extends MmaFighter { - public MmaHeavyweightFighter(String name, String surname, String nickName, String speciality) { - super(name, surname, nickName, speciality); - } + public MmaHeavyweightFighter(String name, String surname, String nickName, String speciality) { + super(name, surname, nickName, speciality); + } } ``` -A fighter is allowed to fight an opponent of the same weight classes, if the opponent is of a different weight class -there is an error +A fighter is allowed to fight an opponent of the same weight classes. If the opponent is of a different weight class, an error is raised. ```java -MmaBantamweightFighter fighter1 = new MmaBantamweightFighter("Joe", "Johnson", "The Geek", "Muay Thai"); -MmaBantamweightFighter fighter2 = new MmaBantamweightFighter("Ed", "Edwards", "The Problem Solver", "Judo"); -fighter1.fight(fighter2); // This is fine +MmaBantamweightFighter fighter1=new MmaBantamweightFighter("Joe","Johnson","The Geek","Muay Thai"); + MmaBantamweightFighter fighter2=new MmaBantamweightFighter("Ed","Edwards","The Problem Solver","Judo"); + fighter1.fight(fighter2); // This is fine -MmaHeavyweightFighter fighter3 = new MmaHeavyweightFighter("Dave", "Davidson", "The Bug Smasher", "Kickboxing"); -MmaHeavyweightFighter fighter4 = new MmaHeavyweightFighter("Jack", "Jackson", "The Pragmatic", "Brazilian Jiu-Jitsu"); -fighter3.fight(fighter4); // This is fine too + MmaHeavyweightFighter fighter3=new MmaHeavyweightFighter("Dave","Davidson","The Bug Smasher","Kickboxing"); + MmaHeavyweightFighter fighter4=new MmaHeavyweightFighter("Jack","Jackson","The Pragmatic","Brazilian Jiu-Jitsu"); + fighter3.fight(fighter4); // This is fine too -fighter1.fight(fighter3); // This will raise a compilation error + fighter1.fight(fighter3); // This will raise a compilation error ``` ## Class diagram @@ -117,22 +135,45 @@ fighter1.fight(fighter3); // This will raise a compilation error ## Applicability -Use the Curiously Recurring Template Pattern when - -* You have type conflicts when chaining methods in an object hierarchy -* 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 +* When you need to extend the functionality of a class through inheritance but prefer compile-time polymorphism to runtime polymorphism for efficiency reasons. +* When you want to avoid the overhead of virtual functions but still achieve polymorphic behavior. +* In template metaprogramming to provide implementations of functions or policies that can be selected at compile time. +* You have type conflicts when chaining methods in an object hierarchy. +* 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. * You want certain methods to work only with instances of the same type, such as for achieving mutual comparability. ## Tutorials * [The NuaH Blog](https://nuah.livejournal.com/328187.html) -* 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) +* 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) ## Known uses +* Implementing compile-time polymorphic interfaces in template libraries. +* Enhancing code reuse in libraries where performance is critical, like in mathematical computations, embedded systems, and real-time processing applications. * [java.lang.Enum](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Enum.html) +## Consequences + +Benefits: + +* Elimination of virtual function call overhead, enhancing performance. +* Safe reuse of the base class code without the risks associated with multiple inheritances. +* Greater flexibility and extensibility in compile-time polymorphism scenarios. + +Trade-offs: + +* Increased complexity in understanding and debugging due to the interplay of templates and inheritance. +* Can lead to code bloat because each instantiation of a template results in a new class. +* Less flexibility compared to runtime polymorphism as the behavior must be determined entirely at compile time. + +## Related Patterns + +* [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. +* [Strategy](https://java-design-patterns.com/patterns/strategy/): CRTP can implement compile-time strategy selection. +* [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. + ## Credits +* [Effective Java](https://www.amazon.com/gp/product/0134685997/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0134685997&linkCode=as2&tag=javadesignpat-20&linkId=4e349f4b3ff8c50123f8147c828e53eb) * [How do I decrypt "Enum>"?](http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeParameters.html#FAQ106) -* Chapter 5 Generics, Item 30 in [Effective Java](https://www.amazon.com/gp/product/0134685997/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0134685997&linkCode=as2&tag=javadesignpat-20&linkId=4e349f4b3ff8c50123f8147c828e53eb)