🔍目的
允许派生组件从与派生类型兼容的基本组件继承某些功能。
🔍解释
真实世界例子
对于正在策划赛事的综合格斗推广活动来说,确保在相同重量级的运动员之间组织比赛至关重要。这样可以防止体型明显不同的拳手之间的不匹配,例如重量级拳手与雏量级拳手的对决。
通俗描述
使类型中的某些方法接受特定于其子类型的参数。
维基百科
奇异递归模板模式(curiously recurring template pattern,CRTP)是C++模板编程时的一种惯用法:其中类X派生自使用X本身作为模板参数的类模板实例化。
程序实例
定义一个通用接口Fighter
public interface Fighter<T> {
void fight(T t);
}
定义MmaFighter类用于实例化按重量级别区分的拳手
public class MmaFighter<T extends MmaFighter<T>> implements Fighter<T> {
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;
}
@Override
public void fight(T opponent) {
LOGGER.info("{} is going to fight against {}", this, opponent);
}
@Override
public String toString() {
return name + " \"" + nickName + "\" " + surname;
}
定义MmaFighter 的一些子类型
class MmaBantamweightFighter extends MmaFighter<MmaBantamweightFighter> {
public MmaBantamweightFighter(String name, String surname, String nickName, String speciality) {
super(name, surname, nickName, speciality);
}
}
public class MmaHeavyweightFighter extends MmaFighter<MmaHeavyweightFighter> {
public MmaHeavyweightFighter(String name, String surname, String nickName, String speciality) {
super(name, surname, nickName, speciality);
}
}
效果: 允许拳手与相同重量级的对手交手,如果对手是不同重量级,则会出现错误
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
fighter1.fight(fighter3); // This will raise a compilation error
🔍类图
🔍适用场景
在以下情况下使用CRTP
- 在对象层次结构中链接方法时存在类型冲突
- 您想使用一个参数化的类方法,该方法可以接受类的子类作为参数,从而可以应用于继承自类的对象
- 您希望某些方法仅适用于相同类型的实例,例如实现相互比较。