Thursday, April 30, 2009

Struggling with Java Generics

The idea of Java generics looks straightforward. Really, templates exist in C++ for years, and one doesn't expect too much trouble with much simpler syntax of Java generics. But devil is in details. The major caveat is verifiability requirement.

Look at this [oversimplified] C++ design:

// Class that creates copies of objects of
// given typename T
template<typename T>
class Copier {
private:
    T* t;
public:
    Copier() : t(NULL) {}
   
    T* getCopy() {
        if(t == NULL) {
            t = new T();
        }
        return t->createCopy();
    }
};
   
// Class that is capable to create
// copies of objects of more than one type;
// it uses Copier as a building block
template<typename A, typename B>
class CopyMaster {
private:
    Copier<A> copierA;
    Copier<B> copierB;
public:
    A* getA() {
        return copierA.getCopy();
    }
    B* getB() {
        return copierB.getCopy();
    }
};
   
// Two concrete classes Dolly and Lizard
// can create copies of themselves and are
// ready to be used in Copier<>
class Dolly {
public:
    Dolly* createCopy() {
        return new Dolly();
    }
};
   
class Lizard {
public:
    Lizard* createCopy() {
        return new Lizard();
    }
};
   
   
int main(int argc, char* argv[]) {
    // This particular instance of CopyMaster
    // can create copies of sheep and reptiles
    CopyMaster<Dolly, Lizard> copyMaster;
    Dolly* concrete1 = copyMaster.getA();
    Lizard* concrete2 = copyMaster.getB();
    return 0;
}

The challenge is to achieve the same functionality with Java generics.

No comments:

Post a Comment