If I have a seperate class for my db calls, and I create another implementation of the db layer but say with a different data store.
Is there a way for me to completly swap out the implementation without having to change allot of code?
i.e. I am starting a project, so I can design things properly to achieve this from the get-go.
Note: I will use this pattern for other parts of the site also, not just the db layer so its not really specific to db layer only.
As long as two modules implement exactly the same interface (classes with the same names, methods, and other attributes, functions with the same names and signatures, …) you can pick one or the other at the time your application is starting up, for example on the basis of some configuration file, and import the chosen one under a fixed name. All the rest of your application can then use that fixed name and, net of the startup code, be blissfully unaware of any shenanigans that may have been done at the start.
For example, consider a simplified case:
Now, all the rest of the application can just
import lang, and it will be getting under that name theitalianmodule, so, when callinglang.greet(), it will get the string'Ciao!'.Of course, in real life you’ll have multiple modules, each with multiple functions, classes, and whatnot, but the general principles stay very similar. Just take special care about modules with qualified names (such as
foo.bar), i.e., modules which must reside in a package (in this case,foo). For those, you can’t just use__import__‘s return value, but must use a slightly more roundabout approach, such as:that is, ignore
__import__‘s return value, and reach right for the value that’s left (with the actual name as the key) in thesys.modulesdictionary — that is the module object you seek, and that you can set back intosys.moduleswith the “fake name” by which all the rest of the application will be able to blissfully import it any time.