Here's an outline of the Aggregation services which will be responsible for taking the one min aggregates from the websocket API and converting them to higher time frames (5min, 15min, 30min, 60min, Day).
I'm using a central class, AggregationScheduler, to manage the cron jobs that trigger timeframe conversions on set intervals during market hours. When the scheduler methods trigger, the AggregationServiceFactory is called and will return timeframe specific services. (FiveMinAggregationService, etc)
The scheduler method will then call another method to retrieve the user's list of watchlist stocks. For each stock, the execute() method, which is defined in the AggregationConversion abstract class, will be called on the timeframe specific aggregation class to handle the conversion process.
This method includes:
- queryAggregates() - Abstract method to fetch 1-min aggregates implemented by each aggregation service.
- convertToHigherTimeFrame() - Abstract method to convert aggregates to a higher time frame, implemented by each aggregation service.
- saveAggregates() - Concrete method that saves the data.
This design uses polymorphism, allowing each timeframe specific service to implement it's own conversion and query logic. The use of a factory class encapsulates the creation/selection of the timeframe specific services while abstracting away the internals from the scheduler.
The original implementation did not include the abstract class/methods or the factory class. I had repeated methods across all timeframe specific aggregation services. HUGE Thanks to Michael Bowman from the Dallas Software Developers Group for pointing out the inefficiencies and suggesting a better approach!
#Java #SpringBoot #Design #OOP #Abstraction #Encapsulation #Polymorphism #DRY