Skip to content

Singleton pattern applied to data sources access#1

Open
JeevenDhanoa wants to merge 2 commits intomasterfrom
data-sources-singleton
Open

Singleton pattern applied to data sources access#1
JeevenDhanoa wants to merge 2 commits intomasterfrom
data-sources-singleton

Conversation

@JeevenDhanoa
Copy link
Owner

@JeevenDhanoa JeevenDhanoa commented Aug 14, 2021

What does this PR do?

This PR restructures the code in app/data/__init__.py to 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 in app/data/__init__.py, making the data sources dictionary private to that class, and updating references to this dictionary and it's accessor method in both app/main.py and app/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 base Singleton class that the DataSources class 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, the DataSources class still has an __init__ method, but since it inherits from the singleton base class, it is guaranteed to only ever create a single instance of DataSources. I created another method, get_instance, which serves as the intended way to access the instance of the DataSources class, 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 DataSources class 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 DataSources class, 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.

@JeevenDhanoa JeevenDhanoa changed the title Data sources singleton Singleton patten applied to data sources access Aug 15, 2021
@JeevenDhanoa JeevenDhanoa changed the title Singleton patten applied to data sources access Singleton pattern applied to data sources access Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant