I set out to create an example or proof of concept of an idea I had and would be interested in receiving constructive feedback on my idea. I needed a Java factory pattern that allowed the end-user of the application to extend it (think add-ons, plug-ins, user contributed content, etc.). So I created a Java factory that uses reflection. While I am doubtful that I am the first to have done this, I have not found [any] examples of it. In my example, I created an abstract class called Vehicle. I then extended Vehicle a number of times (i.e. car, train, boat, airplane, etc.) and placed the subclasses in a separate package. In my example, my factory has a class scanner (does not currently support JAR files) that registers each subclass. Then the factory can be easily called using some class data (i.e. ID, name, whatever) to create a new instance of the subclass. I have included a link to the repo in GitHub (I hope that makes it easier to browse/review). What are your thoughts? Is this practical? Useful? Are there obvious improvements? Easily adapted to support JAR files?
I do want to note that I am aware of Google Guava and the Reflections Library. I wanted to create and example without dependencies on other libraries.
Java Reflection Factory Example (GitHub) https://github.com/MannyPeterson/ReflectionFactory
Thank you.
Aucun commentaire:
Enregistrer un commentaire