I have been scratching my head for a while thinking about the best way to remove singletons I stupidly added in the first place. For background, this application is a stock portfolio tracker. I currently have three classes which I thought would be the only instance:
- Prices (a class holding a list of historical prices, dividends, splits)
- Portfolios (a class managing a collection of portfolio objects with some useful wrapper functions)
- Queries (a class that reads and writes to the database)
To meet user requests, I am adding in the ability to open and save from multiple databases. Each database (Queries class) will theoretically be passed into a Portfolios and Prices' constructor to load the data.
The issue I am facing is that all 3 classes are interconnected. If I open C:\database1.sqlite, I shouldn't be able to mix and match the Prices and Portfolios from C:\database2.sqlite. Also, any edits must be saved down to the proper database.
My initial thought is to create a context object which consists of these classes and pass this around. However, reading http://stackoverflow.com/questions/986865/can-you-explain-the-context-design-pattern-a-bit/986947, I don't think this is the right solution.
How can I remove the singletons, but still keep integrity between the objects? Thanks.