There is one clean, supported, and verifiable solution, but you reject it in your requirements:
Dependency injection looks like an unreasonable load on code invocation (given the bundle) and sacrifices too much clarity for my liking
I will ignore this requirement now. See the end of my answer if you really want to avoid dependency injection (which I do not recommend).
Designing collection objects
- Creating a wrapper around the actual collection (as you already know) is a good idea. This gives you complete control over customer interactions with the collection (for example, regarding blocking).
- Do not put it. Create it so that you can instantiate the collection, use it, and finally delete it. In the end, all collections of the standard library and Qt also work.
- Enter an interface for the collection object.
Design a collection access mechanism
The solution should help ensure thread safety.
This yells for a factory-like intermediary: create a factory that provides access to the collection. factory can decide when to return a new collection or an existing one. Make sure customers return the collection when they are finished so you know how many customers use it.
Now all clients get access to the collection through the factory. They see the interface, not the actual implementation.
Getting factory link
Now that we have introduced the factory, customers no longer need to know access to the collection directly (statically). However, they still need to grab onto the factory.
Make a factory dependency by injecting it into client constructors. This design clearly states that customers are factory dependent. It also allows you to disable the factory during testing, for example. replace it with the layout.
Note that using dependency injection does not mean that you need to use the DI structure. It is important that it has a clean, well-defined compound root .
Avoidance DI
As I said, this is not recommended. DI is the foundation for clean, decoupled designs that emphasize testability.
If you still want to avoid DI, modify the above construct as follows:
- Create a singleton that provides access to the factory.
- Access to the factory through this singleton from all customers.
- Keep collections and factories as they are, i.e. non-static and unaware of any singleton.
Additional notes
Your collections and their use sound just like a repository template . My design suggestions given above correspond to this similarity (for example, access collections in a narrow space and βreturn themβ). I think reading about the repository template will help you set up your design correctly.
theDmi
source share