hgducharme / lichess-bot

A middle-man between your chess engine and lichess.com
GNU General Public License v3.0
1 stars 0 forks source link

Favor composition instead of inheritance with `ContinuousWorker` #16

Closed hgducharme closed 1 year ago

hgducharme commented 1 year ago

Instead of subclassing ContinuousWorker (e.g. EventStreamWatcher and ChallengeStreamWatcher), have ContinuousWorker require a WorkBehavior instance that is then called inside the ContinuousWorker.run() function, resulting in:

def __init__(self, work_behavior):
   self.work_behavior = work_behavior

def run(self):
    while not self.terminate_flag.is_set():
        self.work_behavior.work()

where WorkBehavior is an abstract class acting as an interface, and then subclass this class as new behaviors prop up for continuous workers. So we would have a EventStreamWorkBehavior and a ChallengeStreamWorkBehavior, which implement the WorkBehavior interface, and these behaviors get passed into ContinuousWorker.

This is opposed to what we currently do where EventStreamWorker and ChallengeStreamWorker are children of ContinuousWorker. This new approach will adhere better to SOLID principles sine we're favoring composition (and therefore dependency injection) over inheritance.

hgducharme commented 1 year ago

I guess what i'm calling for is the strategy pattern? Basically outsource the abstract work() function to a strategy class that we call instead

hgducharme commented 1 year ago

Nevermind, scratch this idea

Screen Shot 2023-04-23 at 1 59 22 PM