June 2018
Intermediate to advanced
280 pages
7h 46m
English
We continue the previous code by adding a BikeFactory from where customers can select a small bike or a large bike. We can do this without creating a separate class file; we can simply create an anonymous class that extends the VehicleFactory directly in the client code:
VehicleFactory bikeFactory = new VehicleFactory() { @Override protected Vehicle createVehicle(String size) { if (size.equals("small")) return new MountainBike(); else if (size.equals("large")) return new CityBike(); return null; }};bikeFactory.orderVehicle("large", "blue");
Read now
Unlock full access