# The Strategy Pattern Will Make Your Python Code CLEANER

![rw-book-cover](https://i.ytimg.com/vi/hVLb3-OE3pM/maxresdefault.jpg)

## Metadata
- Author: [[Isaac Harris-Holt]]
- Full Title: The Strategy Pattern Will Make Your Python Code CLEANER
- Category: #articles
- Document Tags: [[dev]] [[dev/design-patterns]] 
- URL: https://www.youtube.com/watch?v=hVLb3-OE3pM
- Archive: https://web-archive.alecodes.page/bookmarks?bf=1&search=&title=The%20Strategy%20Pattern%20Will%20Make%20Your%20Python%20Code%20CLEANER
> [!tldr]
> The strategy pattern is a design method that helps organize code by separating different algorithms into distinct classes. This makes code cleaner, more modular, and easier to maintain, allowing for easy changes at runtime without extensive conditional statements. By using this pattern, developers can enhance their code's flexibility while keeping it readable and manageable.

## Highlights
the strategy pattern is a design pattern that allows you to cleanly separate different algorithms and behaviors into separate classes.
__Each algorithm implements a common interface__ so you can easily switch between them at runtime without having to modify your core code. [View Highlight](https://read.readwise.io/read/01j5qs5ace2bghpggy8trfaz6r))
> [!note]
> Is this kinda the same as the factory class?

we're going to use Python's protocol typeint protocol is like an interface in other languages it allows you to define the methods an object
should have without knowing the implementation details up front [View Highlight](https://read.readwise.io/read/01j5qsbjkgnknchvq55mj1eec6))

you may be wondering why you might pick Python's protocol over using an abstract base
class or ABC unlike ABC's Protocols are duct typed meaning you don't have to explicitly inherit from them in order to implement them [View Highlight](https://read.readwise.io/read/01j5qsjaf9qdk5a5d46drkv43e))

Protocols are duct typed [View Highlight](https://read.readwise.io/read/01j5qsr9779888tpa2mp6w0438))
> [!note]
> In "The Strategy Pattern Will Make Your Python Code CLEANER," Isaac Harris-Holt uses the term "Protocols are duct typed" to describe how Python's protocol mechanism allows objects to be used interchangeably based solely on their method signatures, rather than requiring explicit inheritance from a protocol. This flexibility enables developers to integrate external libraries or new classes into their code without needing to modify existing structures, as long as the new objects conform to the expected method definitions. Essentially, duct typing promotes a more modular and adaptable coding style, where compliance with an interface is determined by the presence of specified methods rather than formal inheritance.

the power of the single method interface it allows you to write modular extensible and
flexible code it's easily testable and very readable [View Highlight](https://read.readwise.io/read/01j5qsr0gh3pvf1w3j0jtqm0ez))

you'll see this pattern used a lot particularly in languages like go where the standard library has single method interfaces like io.writer and io.reader the truly powerful thing here is that these interfaces are used by the standard Library so if you implement a read method on your struct you can use it in standard Library functions without having to do any manual type conversions [View Highlight](https://read.readwise.io/read/01j5qswey6zs8mdgxjnv71v4gy))
> [!note]
> Usages

