June 2018
Intermediate to advanced
280 pages
7h 46m
English
In the previous code, we used reflection to instantiate new vehicles. If we have to avoid reflection, we can use a similar factory where to register the new vehicle classes the factory should be able to create. Instead of adding classes to the map, we are going to add instances of each type of object we want to register. Each product will be able to create a new instance of itself.
We start by adding an abstract method in the base Vehicle class:
abstract public Vehicle newInstance();
For each product, the method declared abstract in the base class must be implemented:
@Overridepublic Car newInstance() { return new Car();}
In the factory class, we are going to change the map ...
Read now
Unlock full access