Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Dynamic Factory
    primarykey
    data
    text
    <p>I only have about half a year of java experience, so please excuse my lack of knowledge. I tried doing some google searches, and I have some ideas on this, there are just a few holes that I need to fill in.</p> <p>I am trying to create a 'Dynamic Factory' for a program I am doing. The basis of this is that I want people to be able to insert class files into a folder, and they will be loaded into my factory to create new instances of them later.</p> <p>This is what I have right now:</p> <pre><code>import java.util.HashMap; import java.util.Map; public class KitManager { private Map&lt;String, Class&lt;? extends Kit&gt;&gt; kitMap; public KitManager() { kitMap = new HashMap&lt;String, Class&lt;? extends Kit&gt;&gt;(); } public Kit makeKit(String kitname) { try { return kitMap.get(kitname).newInstance(); } catch (Exception e) { e.printStackTrace(); return null; } } } </code></pre> <p>Now what I am trying to figure out is how to get all the classes inside of a folder, make sure that they extend Kit, and then insert them into the Map to have instances of them created later. This is where I am stuck, I cannot think of a good way to have the classes loaded into the map without having the class names and lookup strings identified somewhere in a config. The idea that I had was to have static code inside each class that would insert itself into the HashMap, but I am not exactly sure how that would work. All I am really asking for is an idea on how to go about accomplishing my goal. (Having a folder that users can input classes into (that extend Kit), and then have those classes dynamically load into my kitMap when my program is loaded)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload