Factory method
Under some scenarios, a class cannot predict exactly what objects it will create, or its subclasses may want to create more specified versions of these objects. Then, the Factory Method Pattern can be applied.
The following picture shows the possible structure of the Factory Method Pattern applied to creating rockets:
A factory method is a method of a factory that builds objects. Take building rockets as an example; a factory method could be a method that builds either the entire rocket or a single component. One factory method might rely on other factory methods to build its target object. For example, if we have a createRocket
method under the Rocket
class, it would probably call factory methods like createStages
and createPayload
to get the necessary components.
The Factory Method Pattern provides some flexibility upon reasonable complexity. It allows extendable usage by implementing (or overriding) specific factory methods. Taking createStages
method, for example, we can...