jeudi 12 février 2015

How to pass specialized arguments to a factory template?

Imagine you have a base class (eg Animal) and a factory (AnimalFactory) using a template method to execute the same operations (using different details) to create an instance. AnimalFactory accepts AnimalDetails to be able to build up the animal:


I use pseudo-code to make to problem generic enough.



class Animal {}
class AnimalDetails {}
class AnimalFactory {

makeBaby:Animal(details:AnimalDetails) { /* some special action */ }

createTemplate:Animal (details:AnimalDetails) {
makeBaby(details)
return baby
}

}


Then I create a subclass Bird and Mammal with their factories and specialized params:



class Bird:Animal {}
class BirdDetails:AnimalDetails {}

class Mammal:Animal {}
class MammalDetails:AnimalDetails {}

class BirdFactory:AnimalFactory {

makeBaby:Bird(details:BirdDetails) { /* bird specific action using bird details */

}
class MammalFactory:AnimalFactory {

makeBaby:Mammal(details:MammalDetails) { /* mammal specific action using bird details */

}


This is obviously not type correct, as the template method (AnimalFactory.createTemplate) is expecting an AnimalDetails parameter.


I've spent some time to come up with type correct solutions that is using the template and flexible on the input type (as far as it's fitting the factory subclass methods) - but nothing was good enough. Could you think of a better way to deal with this issue?


Aucun commentaire:

Enregistrer un commentaire