This example demostrates two related design patterns:
-
Factory Method creates an object without exposing the creation code to client, and the client uses the same common interface to create new type of object.
-
Abstract Factory produce families of related objects without specifying the concreate classes.
World
creates different types of Vehicle
without knowing the details of how
they are created. Instead, VehicleFactory
takes care of the creation details.
This helps us decouple the creation code from the user.
Note that Car
and Bike
are created differently in the code as Bike
needs
additional parameters. With factory method, we hide these details inside the
factory. As a result, the caller, World
, doesn't have to know about it.