Design patterns – Factory method


Hello everyone,

I will start explaining (and implementing) the design patterns, and i’ll do it first with the Factory Method.
Factory method is part of the creational patterns, and it is done using a function that will create an object based on a parameter given at runtime (usually), and returning a pointer to the base class.
This implies that all the objects returned by our factory method must inherit from the same base class.


Assuming we have:

  • base class (called Base)
  • two classes that inherits it:  Derived1 (with the unique ID = 1) and Derived2 (with the unique ID = 2)

The factory method will look like this:

A more detailed code can be found at the following address:

https://github.com/badearobert/cplusplus/tree/master/DesignPatterns_Factory

Best regards,
Robert

Leave a comment

Your email address will not be published.