faif / python-patterns

A collection of design patterns/idioms in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[question] strategy pattern class methods

arcturus140 opened this issue · comments

hi, thank you for this amazing repository. I don't understand why the strategy pattern is not an abstract class, i.e., using @abc.abstractmethod. Was it done this way to provide a default strategy?

Hi,

The simple answer is because you don't have to do it. Python gives you both options. a) Use the abc module to define a formal interface. b) Be informal and rely on duck-typing. The first option might help you to discover problems earlier (e.g. Pycharm will warn you if you don't conform to an interface). The second option gives you more flexibility when experimenting/prototyping