jeudi 10 décembre 2020

Observer Pattern and SpringBoot @Service

Could anyone help me how to handle observer pattern and SpringBoot @Service?

Let's imagine I have class:

WeatherStation[temperature, humidity, windSpeed ...] - subject (observable)

I can have a lot of these classes and I need to update the data in database, so when temperature changes I need to log it to database under correct WeatherStation.

I came with three solutions, first is:

Create class WeatherStationObserver which would have reference to WeatherStationRepository and I would register one WeatherStationObserver for one WeatherStation and the WeatherStationObserver would save the data to database (this is similar how I have it implemented right now) but there are some caviats. For example I can not @Autowire to WeatherStationObserver and I can not make the methods @Transactional ...

Second idea is:

Create WeatherStationUpdateService which would be called from WeatherStationObserver. I don't like this because the WeatherStationObserver serves here only to call the WeatherStationUpdateService.

Third idea:

Make the service an observer and register it for each WeatherStation but I am not sure if the service should not be called only from outside and if it is good practise service to behave as observer.

Could someone help me here?

Aucun commentaire:

Enregistrer un commentaire