Add a creational pattern #451
Closed
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.
Why I made the specific design decision:
I apply a singleton pattern in the SUD to ensure the LocationService of each data-source has at most one instance and provide a global access point to that instance to avoid bugs arising from multiple instances. And the singleton pattern can ensure only one instance to save memory which can improve software performance. So I chose the singleton pattern.
How I applied the new design:
I used the decorator to implement the singleton pattern in app\services\location_init_.py. Define a dictionary in the decorator to hold instances. Then every time create an instance, check it in the dictionary. If it has already instantiated, take the instance directly. If not, create an instance and store it in the dictionary. Then add “@singleton” to LocationService class to ensure each data-source has at most one instance.