mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-18 04:28:04 +00:00
docs: Content SEO updates (#2990)
* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
---
|
||||
title: Curiously Recurring Template Pattern
|
||||
title: "Curiously Recurring Template Pattern in Java: Leveraging Polymorphism Uniquely"
|
||||
shortTitle: Curiously Recurring Template Pattern (CRTP)
|
||||
description: "Discover the Curiously Recurring Template Pattern (CRTP) in Java. Learn how to achieve static polymorphism for efficient method overriding and compile-time polymorphic behavior. Perfect for performance-critical applications."
|
||||
language: en
|
||||
category: Structural
|
||||
tag:
|
||||
- Code simplification
|
||||
- Extensibility
|
||||
- Generic
|
||||
- Idiom
|
||||
- Instantiation
|
||||
- Polymorphism
|
||||
- Recursion
|
||||
- Code simplification
|
||||
- Extensibility
|
||||
- Generic
|
||||
- Idiom
|
||||
- Instantiation
|
||||
- Polymorphism
|
||||
- Recursion
|
||||
---
|
||||
|
||||
## Also known as
|
||||
@@ -20,29 +22,25 @@ tag:
|
||||
* Recursive Generic
|
||||
* Static Polymorphism
|
||||
|
||||
## Intent
|
||||
## Intent of Curiously Recurring Template Pattern
|
||||
|
||||
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.
|
||||
The Curiously Recurring Template Pattern (CRTP) is a powerful design pattern in Java used to achieve static polymorphism. By having a class template derive from a template instantiation of its own class, CRTP enables method overriding and compile-time polymorphic behavior, enhancing efficiency and performance in your Java applications.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Curiously Recurring Template Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
> 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.
|
||||
>
|
||||
> 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.
|
||||
>
|
||||
> 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.
|
||||
> 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. By applying the Curiously Recurring Template Pattern (CRTP) in Java, you can create a base template class `MediaItem` encompassing these common methods. Each specific media type (e.g., `Book`, `DVD`, `Magazine`) would inherit from `MediaItem` using itself as a template parameter. This approach allows each media type to customize shared functionality efficiently, avoiding the overhead of virtual methods.
|
||||
|
||||
In plain words
|
||||
|
||||
> Make certain methods within a type to accept arguments specific to its subtypes.
|
||||
> The CRTP in Java ensures that certain methods within a type can accept arguments specific to its subtypes, enabling more efficient and type-safe polymorphic behavior at compile time.
|
||||
|
||||
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.
|
||||
|
||||
**Programmatic example**
|
||||
## Programmatic example of CRTP in Java
|
||||
|
||||
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.
|
||||
|
||||
@@ -115,7 +113,7 @@ Program output:
|
||||
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)
|
||||
```
|
||||
|
||||
## Applicability
|
||||
## When to Use the Curiously Recurring Template Pattern in Java
|
||||
|
||||
* 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.
|
||||
@@ -124,17 +122,17 @@ Program output:
|
||||
* 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
|
||||
## Curiously Recurring Template Pattern Java Tutorials
|
||||
|
||||
* [Curiously Recurring Template Pattern in Java (The NuaH Blog)](https://nuah.livejournal.com/328187.html)
|
||||
|
||||
## Known uses
|
||||
## Real-World Applications of Curiously Recurring Template Pattern in Java
|
||||
|
||||
* 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.
|
||||
* Implementation of the `Cloneable` interface in various Java libraries.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Curiously Recurring Template Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -148,13 +146,13 @@ Trade-offs:
|
||||
* 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
|
||||
## Related Java Design 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
|
||||
## References and Credits
|
||||
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||
* [Effective Java](https://amzn.to/4cGk2Jz)
|
||||
|
||||
Reference in New Issue
Block a user