Singleton pattern applied to data sources access#1
Open
JeevenDhanoa wants to merge 2 commits intomasterfrom
Open
Singleton pattern applied to data sources access#1JeevenDhanoa wants to merge 2 commits intomasterfrom
JeevenDhanoa wants to merge 2 commits intomasterfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR restructures the code in
app/data/__init__.pyto implement a singleton pattern for a new class,DataSources, through which clients can retrieve a list of all data sources or a specific data source. I made these changes by adding this DataSources class inapp/data/__init__.py, making the data sources dictionary private to that class, and updating references to this dictionary and it's accessor method in bothapp/main.pyandapp/routers/v2.py. I updated the existing accessor method, and added a new one to accommodate code in v2.py which required access to the entire dictionary.I then created a new util file,
singleton.py, which defines a baseSingletonclass that theDataSourcesclass inherits from. This base singleton is taken from the python documentation, which states that the singleton pattern can be implemented in python in this way, by overriding the__new__method. Since there is no way to truly make methods private in python, theDataSourcesclass still has an__init__method, but since it inherits from the singleton base class, it is guaranteed to only ever create a single instance ofDataSources. I created another method,get_instance, which serves as the intended way to access the instance of theDataSourcesclass, by only ever returning a reference to a single instance of the class.Why make these changes?
Since multiple parts of the code (main, routers/v2) all need access to the different location services, it seems intuitive to encapsulate these services into a single class, and then maintain only a single instance of the class, through which the location services can be accessed. And this, of course, can be done through the singleton pattern. Creating a singleton and using it in this way helps maintain the constraint that only one location service will be used at any given time, since it will be impossible for multiple instances of the
DataSourcesclass to exist, and since the main accessor method of the aggregate root will only return a single location service.Additionally, applying the singleton pattern here will help save memory, since instead of multiple instances and multiple location services existing, there will just be one instance of the
DataSourcesclass, which itself only stores single instances of the location service classes. This also takes the responsibility of managing these multiple instances away from the parts of code that instantiate them, since there will only ever be one instance accessible.