I find myself in the situation that many times I would like to encapsulate the logic for the creation of an object in an specific class and I don't know if there is any pattern that matches what I want to do. This is, always creating from some input parameters the same type of object, without need for dynamically configuring options.
The three candidates I consider are Factory (all it's variants), Builder and Mapper.
In general, all factory patterns state that a factory does not return an specific class but an interface, is then a sub-class or the class that implements the factory the one that decides which class that implements the interface should instantiate. I would like to create always the same class type so I don't need to many subclassses or implementations that would return different types of implementations of an interface, that's why I think it's not a factory.
Builder pattern peculiarity is to use methods to be able to expose multiple configuration options, and finally creating the object after doing all the configuration. I don't need this because I would just need a Create method that would always do the same without configuration possibilities.
A Mapper I understand only maps between different properties from one object to another, when the creation of the object is more complex and involves logic or calls to external components I would not refer to it as a mapper because it would be confusing and may lead to confusion expecting to have a simple mapping on it.
I think it's very common to face this situation and I'd like to know if there is some pattern that describes exactly what I do, or which one is usually used for this purpose.
Aucun commentaire:
Enregistrer un commentaire