Skip to content

nanchano/dynamic-factory-pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dynamic Factory Design Pattern

The factory pattern might be one of the most common and well known design patterns, and for good reasons. It relies on abstractions while being able to serve clients with concrete implementations of the object they want.

Usually, the pattern relies on if or switch/match chains to determine the object to return, which can work when there are a limited number of objects, but it presents some problems when scaling, as forgetting to add the condition to the factory ends up creating unexpected side effects.

An alternative is just making a dynamic factory, which sacrificies readability for scalability, while also introducing some conditions in the way the project, modules and objects should be structure or named.

It's especially easy to implement in Python by leveraging the getattr function and the importlib module, as we can dynamically look for objects in any module we want.

For a walkthrough of the example, head to my blog post

About

The classic Factory Design Pattern, with a slight twist to make it dynamic.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages