I implemented The Observer Design Pattern to the project's _update() method.
Why is this important?
As of now, the project is quite small and does not offer many extras features. However, If the project were to be extended, the observer pattern makes possible several features. An observer pattern primarily is a "reactor"; when some objects change, other objects monitoring that object can react to the change in some ways.
How
In the original project, previousData would be updated to the old value of latestData everytime new data for latestData was fetched. This is where observer pattern comes into play. Setting latestData as the publisher and previousData as a subscriber, everytime latestData is changed, previousData is updated correspondingly.
To achieve this publisher-subscriber relationship, I created a new class GenericPublisher in publisher.py; this class represents a general class. Second, created another class LatestDataPublisher that extends the GenericPublisher class. LatestDataPublisher holds the latestData. Third, I created a new class PreviousDataSubscriber, which represents the previousData we previously had. Now, i set up the publisher-subscriber relationship in the COVID19 class, creating references to LatestDataPublisher (latestDataPublisher) and PreviousDataSubscriber (previousDataObserver) in the constructor. Finally, whenever _update() is called, to update the what was previously latestData (now latestDataPublisher.latestData), set_latestData() method is called with the value to update it with and notifyObservers() is called to update previousDataObserver.previousData .
Test
To ensure the changes work correctly, I created a test file test_observer_pattern.py. The tests were written using pytest, as such requirements.txt has been updated.
Is there any existing behaviour change due to this modification?
Applied The Observer Pattern
I implemented The Observer Design Pattern to the project's
_update()
method.Why is this important?
As of now, the project is quite small and does not offer many extras features. However, If the project were to be extended, the observer pattern makes possible several features. An observer pattern primarily is a "reactor"; when some objects change, other objects monitoring that object can react to the change in some ways.
How
In the original project,
previousData
would be updated to the old value oflatestData
everytime new data forlatestData
was fetched. This is where observer pattern comes into play. SettinglatestData
as the publisher andpreviousData
as a subscriber, everytimelatestData
is changed,previousData
is updated correspondingly.To achieve this publisher-subscriber relationship, I created a new class
GenericPublisher
inpublisher.py
; this class represents a general class. Second, created another classLatestDataPublisher
that extends theGenericPublisher
class.LatestDataPublisher
holds thelatestData
. Third, I created a new classPreviousDataSubscriber
, which represents thepreviousData
we previously had. Now, i set up the publisher-subscriber relationship in theCOVID19
class, creating references to LatestDataPublisher (latestDataPublisher
) and PreviousDataSubscriber (previousDataObserver
) in the constructor. Finally, whenever_update()
is called, to update the what was previouslylatestData
(nowlatestDataPublisher.latestData
),set_latestData()
method is called with the value to update it with andnotifyObservers()
is called to updatepreviousDataObserver.previousData
.Test
To ensure the changes work correctly, I created a test file
test_observer_pattern.py
. The tests were written usingpytest
, as suchrequirements.txt
has been updated.Is there any existing behaviour change due to this modification?
No, everything works the way it was.