Abstract Factory for Python
Copyright (c) 2016-2017 David Betz
See test_provider.py unit test for usage.
Basically an implementation of an abstract factory pattern.
In one system where I use this, I create factories for eachs type of thing in my system. So, SearchFactory, CloudStorageFactory, QueueFactory, AristotleFactory, etc… These would implement for ID interface like ICloudStorageProvider (in Node, it’s just a class).
Each of these would have their own switch/case (or whatever) to create the factory for it. So, for example, I may have config in a YAML file specifying that I want to use Mongo for my Aristotle provider (“Aristotle” is what most people incorrectly call “NoSQL”).
To begin, create the factory (do this one for the entirety of your system):
abstractFactory = AbstractFactory()
Then, add your factories:
abstractFactory.set(SearchFactory) abstractFactory.set(CloudStorageFactory) abstractFactory.set(QueueFactory) abstractFactory.set(AristotleFactory)
When the time comes, just ask for your provider:
provider = abstractFactory.resolve(IAristotleProvider)
Your code SHOULD. NOT. CARE. ABOUT. MONGO. It should the your configuration or something handle that. Don’t tightly couple your providers.
Also note that the resolver also accepts various arguments for extra flexibility:
provider = abstractFactory.resolve(IAristotleProvider, "alternateConnectionString", collection="log")
Despite what random bloggers say, service locators are awesome and provide excellent decoupling.
Look at the Mock examples provided with the tests; they’re rather extensive.
Metadata
Release files for abstractfactory 1.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| abstractfactory-1.0.3.tar.gz | 4.5 kB | Without provenance | Details for abstractfactory-1.0.3.tar.gz |
Release files / abstractfactory-1.0.3.tar.gz
| Download URL | abstractfactory-1.0.3.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
712eb72e138994a1de406c16c3574ed5dc6447f1b2e61236be63b7ea134ebefe
|
|
BLAKE2b-256 checksum How to use checksums |
fd02b9b4328ff23342300c9ce11ccbd82a98ce4b90ded760914395817e67ba32
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for abstractfactory 1.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.