State Design Pattern

preview_player
Показать описание
More if/else if or switch statements your code has, the more complicated and prone to errors it becomes.
Changing anything or adding new features becomes really hard, especially in codebases with no unit test coverage.

If the conditions you're checking against verify that your object is in a particular state, then look no further than using the State Pattern.
The idea is that your object becomes a context, which is basically a placeholder for the current state, as well as storage for all possible states.
And then for each state your object can be in, you implement its own State class.

Watch this 3.5-minute video for a quick explanation of the State pattern and its implementation in Python.

#Python #DesignPatterns #StatePattern

-------------------------------------------------------------------------------------
📰 *Sign up for my newsletter*

🖥️ *Free Courses to Learn Programming*

🛒 *Products I recommend*

*Connect with me*

#codevev
Рекомендации по теме
Комментарии
Автор

What's the longest chain of if/elif statements in your codebase?

codevev
Автор

UML diagram looks like strategy pattern. Instead of handle, it becomes behavior. Would you elaborate on this?

yinmudino
Автор

This video kept running. Like really fast. Code popped on screen and vanished. While example is really good, video speed does not do justice to it. Can you kindly do the video with more code screen time and step by step explanation?

AmitabhSuman
Автор

i wish you had to learn it from your this video

chandrikasaha