June 2018
Intermediate to advanced
280 pages
7h 46m
English
For this method, we are going to use a map to keep the product IDs along with their corresponding classes:
private Map<String, Class> registeredProducts = new HashMap<String,Class>();
Then, we add a method to register new vehicles:
public void registerVehicle(String vehicleId, Class vehicleClass){ registeredProducts.put(vehicleId, vehicleClass);}
The create method becomes the following:
public Vehicle createVehicle(String type) throws InstantiationException, IllegalAccessException{ Class productClass = registeredProducts.get(type); return (Vehicle)productClass.newInstance();}
In certain situations, working with reflection is either impossible or discouraged. Reflection requires a runtime ...
Read now
Unlock full access