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