jeudi 15 octobre 2015

Design for converting objects with mirroring class hierarchy

I have a low-level (i.e. cannot depend on higher-level modules) MySQL DAO package that returns polymorphic objects

abstract class MySQLAnimal {
    int a;
}

class MySQLCat extends MySQLAnimal {
    int b;
}

Collection<MySQLAnimal> retrieveAllMySQLAnimals(...) {...}

Then, I have a consumer of this package that provides abstraction (i.e. it's agnostic of how they are retrieved) and contains classes with mirroring class hierarchy:

abstract class Animal {
    int a;
}

class Cat extends Animal {
    int b;
}

Collection<Animal> retrieveAllAnimals(...) {...}

I'm trying to write an adapter in this consumer package that needs to retrieve MySQLCat object using MySQL DAO and return it in the form of abstracted class (i.e. implementing the retrieveAllAnimals(...) method). What would be the cleanest way (read: without having to call instanceof) to do this?

Aucun commentaire:

Enregistrer un commentaire