I have a Product class that contains methods and a complex data structure. I will have 8 types of products that differ only in the contents of the data structure, nothing else. In my application I will then need to create one instance of each of the 8 types of product, once, and the types of products that exist do not change at run-time.
What is the best approach and why?
A) Class Product has 8 sub-classes. Each of these sub-classes defines only the constructor. Inside each constructor the data structure is properly created for that type of product.
B) A Factory class has 8 sub-classes. Each of these sub-classes is a concrete factory for each of the 8 types of products. Class Product has no sub-classes. Each concrete factory creates the specific type of product by creating the appropriate data structure and passing it as an argument to the constructor of class Product.
I have been reviewing the advantages of the Factory design pattern and I can't see, for this specific case, any of those advantages in B over A. Am I missing something?
Aucun commentaire:
Enregistrer un commentaire