lundi 20 avril 2020

How to implement factory pattern that dynamically loads new products (open close principle)?

In factory method, we have to write switch case or if statements to decide which instance to create and return. This violates the open close principle because - everytime a new product is to be added, the factory method code has to be updated.

Is there any way to make this dynamic - so that adding a new product does not require any change to the factory method?

For exmaple: In python we can import package that has list of all available products. The factory method loads the dict data structure with all available products along with their class. Hence the factory code need not change when new product is added. It is sufficient that only the product is added to the package code that the factory uses to import the products classes.

Aucun commentaire:

Enregistrer un commentaire