I got a few classes handling different file extensions. They all got implement a common interface or extend the same abstract class. I want to dynamically (runtime) get the correct subclass depending on the file's extension.
Now I'm a bit stuck where to place the logic to select the correct implementation. After thinking a bit I would now create a dict inside the parent class doing the selection. e.g.
{"jpg": jpgProcessor,
"gif". gifProcessor}.get(fileExt)().process(file)
In Java I would create a register method inside the parentClass which gets invoked by a static block of the children classes. The parentClass would then have a getFilter(String fileExt) method which selects the correct Class out of a HashMap or something similar.
Questions:
- a) Is this the correct way to implement this in Java?
- b) Is there a better way to implement something like this in Python?
- bonus) Is there a concrete pattern for this? How is it called?
I always read many articles about factory, abstract factory patterns but was pretty unsure if i could replace the type checks inside the parent class.
Aucun commentaire:
Enregistrer un commentaire