No need to subclass
The reduction in classes is another big benefit of using a virtual constructor. In the case above, we only needed a single SpawnShape instead of duplicating our spawn class or making derived versions. Consider our factory builder classes that we saw before. We were forced to create an abstract base class and create derived classes for every new M5Component and M5Stage that we used. C++ templates helped us autogenerate that code, but the code still exists.
By using a virtual constructor, we don't need derived builder classes for every M5Stage, M5Component, Shape, or other inheritance hierarchy. We can let the objects copy themselves. Does that mean we should remove our factories and always use the Prototype pattern? It ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access