The Artima Developer Community
Sponsored Link

Computing Thoughts
Curiously Recurring Generic Pattern
by Bruce Eckel
October 21, 2005
Summary
In my posting "Mixins: Something Else You Can't Do With Java Generics?", someone suggested (incorrectly) that this was just the C++ "Curiously Recurring Template Pattern." The analog of the CRTP does work in Java, but is it good for anything?

Advertisement

Although you can't inherit directly from a generic parameter, you can inherit from a class that uses that generic parameter in its own definition. That is, you can say:


//: generics/CuriouslyRecurringGeneric.java

class Generic<T> {}

public class CuriouslyRecurringGeneric
  extends Generic<CuriouslyRecurringGeneric> {} ///:~
This could be called the "Curiously-Recurring Generic Pattern" (CRGP) after Jim Coplien's "Curiously-Recurring Template Pattern" in C++. The "curiously-recurring" part refers to the fact that your class appears, rather curiously, in its own base class. The motivation for the CRGP is to express a form of the Template Method design pattern. The derived class, because it is used in the generic definition of its own base class, must conform to the structure of the base class, which means it must define, for example, any abstract methods that may be in that base class, and it can choose to override other non-abstract methods. Here's a simple example:

//: generics/GenericTemplateMethod.java

abstract class GenerifiedTemplate<T> {
  public void templateMethod() { f(); g(); }
  public abstract void f();
  public abstract void g();
}

class Subtype extends GenerifiedTemplate<Subtype> {
  public void f() { System.out.println("f()"); }
  public void g() { System.out.println("g()"); }
}

public class GenericTemplateMethod {
  public static void main(String[] args) {
    new Subtype().templateMethod();
  }
} /* Output:
f()
g()
*///:~
What does this buy you over simply inheriting from an ordinary base class set up to support the Template Method pattern?

Talk Back!

Have an opinion? Readers have already posted 8 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Bruce Eckel adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Bruce Eckel (www.BruceEckel.com) provides development assistance in Python with user interfaces in Flex. He is the author of Thinking in Java (Prentice-Hall, 1998, 2nd Edition, 2000, 3rd Edition, 2003, 4th Edition, 2005), the Hands-On Java Seminar CD ROM (available on the Web site), Thinking in C++ (PH 1995; 2nd edition 2000, Volume 2 with Chuck Allison, 2003), C++ Inside & Out (Osborne/McGraw-Hill 1993), among others. He's given hundreds of presentations throughout the world, published over 150 articles in numerous magazines, was a founding member of the ANSI/ISO C++ committee and speaks regularly at conferences.

This weblog entry is Copyright © 2005 Bruce Eckel. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use