I am implementing a factory pattern of sorts with my project. I have a class GeneralFactory
which acts as a base class to SpecificFactory
. Each Factory class creates its own SpecificProduct
. My pattern differentiates from a classic factory pattern in the sense that the base Factory class GeneralFactory
is not a pure abstract class and contains the bulk of the implementation for all Specific Factory classes (there are not many things differentiating Factory classes). The base GeneralFactory
contains a collection of Products
and has a function virtual std::shared_ptr<Product> createProduct(...)
. Each of the SpecificFactory
classes override the implementation to create their own SpecificProduct
.
Is there a way to not need to override virtual std::shared_ptr<Product> createProduct(...)
in GeneralFactory
? I have shared functionality that should execute within createProduct
but I do not want to add that functionality to every SpecificFactory
class's implementation of virtual std::shared_ptr<Product> createProduct(...)
.
I am currently thinking of creating a non-virtual function within GeneralFactory
that will then call the overridden createProduct
function, but is there a better way to do this?
Class Hierarchy
GeneralFactory
--ParentOf--> SpecificFactoryA
GeneralFactory
--ParentOf--> SpecificFactoryB
...
Product
--ParentOf--> SpecificProductA
Product
--ParentOf--> SpecificProductB
...
SpecificFactoryA
--Creates--> SpecificProductA
SpecificFactoryB
--Creates--> SpecificProductB
...
GeneralFactory
--HasA--> Collection<Product>
I am using C++17 if that affects anything.
Aucun commentaire:
Enregistrer un commentaire