👷 Join the FREE Code Diagnosis Workshop to help you review code more effectively using my 3-Factor Diagnosis Framework: www.arjancodes.com/diagnosis
@MrGeordiejonАй бұрын
Totally relevant design pattern. I use one in my python based virtual assistant to speed up and make robust my microphone input code. States: listening, understanding, responding
@ErikBongers Жыл бұрын
I think you forgot to mention that states are really useful for physical machine safety. Robots for example. It avoids having if-red-button-pressed kind of code all over the place, which will guaranteed be forgotten in one place. With proper state design, the red button press will only put the machine in SafetyState, which happens to be a single source file, conveniently listing all possible actions and state transitions, thereby greatly reducing the risk of oversight.
@vyacheslavvorobyov22 Жыл бұрын
If you deal with such a kind of software, I'd advise you to also learn (if already didn't) about "Behavior Tree" approach(just google it). Originally it appeared in a Game Dev, but now it have gained a second life in robotics.
@torwine4895 Жыл бұрын
1987, my first project, TurboPascal, MSDOS, 640 kB ram. It was about translating textual descriptions into graphics for hercules, ega, and printer. I created some integer drawing algorithms and an interpreter for the description language. This part went quite well, i was a bit proud. There was also some user interface part, which made me crazy, because i tried to implement the state changes implicitly, on the fly, by intruducing some variables without any specific concept in my mind. The problem looked so easy, but it didnt work, the logic was distributed and incomplete. I felt quite stupid, that i couldnt get a grip on it. Then i suddenly recognized, that a state machine will solve my problem. It was so easy now. Years later i used statemachines heavily in a WinNT/c++ computer telephony application, to handle the user interface for things like 'Dear Friend, if you want to talk to press 5, else if you ...' etc. We described the states, statechanges, wav-files, options and actions in a config. Technically it was a nice project, multithreaded parallel statemachines that could interact. It's quite cool to be software developer ;-)
@philiperiskallaleal6010 Жыл бұрын
Dear Arjan, thank you for your amazing videos, and once again, a perfect match between didactic and humor material. With respect to the design pattern presented in this video, I would like to discuss in more depth the implications of its use. Especifically, I noticed that this pattern impose a certain degree of redundancy and coding rewriting, since all inherented classes from the abstract (or the Protocol) class must implement its own definitions of each abstract method. This problem became very clear in your Draft/Document coding example. At that example, each class had to implement all methods that the base (abstract) class declared, even in cases in which the derived state class (e.g., Finalized class) does not have any real functionality (method); this Finalized class does basically nothing, since the document is already finalized. From a strongly typed language point of view (e.g., C#, or C++), I see several problems with this pattern; as it stands, there would be a constant need of casting operations in order to overcome that redundancy coding that was mentioned at the beginning of this paragraph. Let me know your comments in this regard. Sincerely,
@SidathWijesooriya Жыл бұрын
For anyone new to state machines, I'd also recommend pytransitions as a library! Cheers!
@gabrielcanuto3321 Жыл бұрын
You're such a great teacher. I really like the way you build a concept from simple examples and explain how they might useful in real cases. Congratulations
@ArjanCodes Жыл бұрын
I'm really happy you found my content helpful, Gabriel! :)
@VivekKumarSinha25 Жыл бұрын
For the first time in last 2 years, I have no idea what the code did, just by looking at it, until a google search. Damn, you surely make advanced videos on Python and the more I dive into it, the more I get to learn, every single time 😅
@ronboprime7 ай бұрын
I'm finally to a point, where I can appreciate your videos! I watched this, about 6 months ago, when I first heard of state machines... and it was mostly lost on me. so I had to put in some work, and now, we're here. This is all, just to say, thank you. Thanks for the help, thanks for the motivation, and thanks for the free education! cheers!
@ArjanCodes7 ай бұрын
It's really nice to see your progress! I love hearing other's learning journeys and wish you success in the future. Thank you for the support :)
@Braekpo1nt6 ай бұрын
I really needed this explanation! I've reached the point in my game development where there are too many if statements to check what state I'm in and perform different actions within each of the public methods. Instead, I can put each of these different behaviors in its own state, and have the context keep track of online players/admins etc.
@lucianop.3922 Жыл бұрын
I have used the state design pattern and also for video games development :) . But like it usually happens I used a variation. I used it for changing a boss' behavior depending on his current phase, so I added a method for checking whether a phase change should happen, and I also added an on_enter method to make the boss make an 'opening' sort of move, signaling the change of phase for the player. I loved it because it makes sense both from the player perspective as well as from the programmer perspective.
@tomvonheill Жыл бұрын
Love it, never knew about this state pattern, thank you for educating the public!
@ArjanCodes Жыл бұрын
Glad it was helpful!
@Carloes Жыл бұрын
Hi Arjen, great video, In the bulb example you do not need the LightState Protocol class at all, it's not being used. I thought for a second that there was some magic going on.
@VivekKumarSinha25 Жыл бұрын
It's defining LightState abstract class.
@palto-ai Жыл бұрын
@@VivekKumarSinha25 Its defined but not used, so the example doesn't make much sense. In my view the Bulb class __init__ should use it in "self.state: LightState = OnState()" to specify that the type of "state" must mat the protocol class.
@VivekKumarSinha25 Жыл бұрын
@@palto-ai It need not be explicitly inherited by the class that implements abstract. As per PEP 544 - in section Defining a Protocol, it's clearly explained. Say a class SupportsClose extends Protocol type hint, and defines a close() method. Quoting "Now if one defines a class Resource with a close() method that has a compatible signature, it would implicitly be a subtype of SupportsClass, since the structural subtyping is used for Protocol types" Python is way more dynamic than we actually think.
@VivekKumarSinha25 Жыл бұрын
@@NathanielKrefman Of course that can be done, but the whole point of type hints in Python is to embed implicit behaviour without explicitly writing code for it. Example is Optional from typing library, once you declare a field as type Optional, python implicitly considers it as an optional value. Similarly when a class extends Protocol, it becomes an abstract class and any methods definitely within it are abstract methods. Now, in that module, any other class that defines a method which has same signature as the method defined in class that inherits Protocol, the former implicitly implements that abstract class.
@BorgPrincess Жыл бұрын
I think defining something without actually referring to it anywhere in the code would have warranted a sentence or two of explanation. It certainly threw me enough to jump back whether I had missed something, and to think for a while: "That is the downside of letting an AI copilot do too much of your coding." 😉
@manonthedollar Жыл бұрын
Been watching since you started your channel! Your design pattern videos are the best. Thank you.
@ArjanCodes Жыл бұрын
I really appreciate the kind words, thank you!
@THElalouloute Жыл бұрын
In the context of automation you will use state patterns to describe a process or a simple machine. And you can use some frameworks. For example in python you can use library like 'transitions' or 'automate'.
@barely3am Жыл бұрын
Thanks!
@ArjanCodes Жыл бұрын
Thank you for the continuous support! :)
@jan.kowalski Жыл бұрын
Seems like you are rewarded by the lines of code :-) For that, such solutions are excellent!
@eltreum1 Жыл бұрын
The only thing outside game engines I have ever worked on a state machine was a custom app our company made to auto discover scan/audit customer computer networks and devices configs. The main thread used states to control the phases of work it had to do discovering and dispatching worker threads until there was no more targets to gather and sanitize info into a sqlite db as a temp cache. It then would export the temp db to CSV files used to generate a website from templates and upload it to an internal web server for users to browse the data and download the CSVs if they needed. It was educational porting it from Py 2.7 to 3.3 a few years ago.
@mrrobotman5299 Жыл бұрын
I found a good place to use the state pattern is in a parser I was writing. The goal is to parser through an xml file and by using states I was able to handle it as a file stream rather than loading the entire file and creating a nested dictionary in memory.
@clonedpraxis7909 Жыл бұрын
13:39 "Animals sometimes must be sacrificed to create all required pairs"
@trupalcanada Жыл бұрын
I failed to understand how this design pattern decouples the classes. For the bulb example, the bulb needs to know about the State classes and the State classes interface needs to know about the bulb class. this is highly coupled from what I see. it does reduce the responsibilty of a class but decoupled.. I'm not able to grasp how.
@lucianop.3922 Жыл бұрын
I think he actually meant to say that with this design pattern you don't have all the code inside the same method, where you need to have access to lots of variables to perform different actions. For example, without the state design pattern, you might write several if/else blocks, checking whether your state is Draft, Reviewed or Finalized, and also whether the action to perform is edit, review or finalize. Personally, I'd say it's a design pattern that helps more with cohesion and modularization rather than decoupling. Another word I'd use is encapsulation.
@WalterVos Жыл бұрын
And it allows you to clearly define the business rules of state changes.
@trupalcanada Жыл бұрын
@@lucianop.3922 that makes sense, thank you
@lucianop.3922 Жыл бұрын
@@NathanielKrefman You raise some interesting points! Actually, I am with you that whenever I can, I choose functions over classes. Sometimes that takes me very far from the oop approach, but it's what my brain understands better, and for personal projects, since I am the one that is going to maintain that code, it's not going to be a good thing if I am not able to understand it now, or a couple of months later, or a couple of years later, let alone if it's a big project (like a game long enough) with many subsystems and components. A quick example I can think of is level logic. Even though they are very similar (same base methods, same base attributes), a functional approach still seems more intuitive to me, and so I write all methods as just functions, and all attributes as just variables (I use Python by the way so I take advantage of the fact that each file is a module, as such, I can use global variables as module attributes, which isn't strictly global as it doesn't really affect variables in other modules). For non-personal projects (other Git open-source projects, work projects, etc.), I try to stick with the coding style of that project though. To avoid confusing myself, I always try to think of different coding styles/programming paradigms as what seems more *readable* for a *given team/project*. Because I believe the truth is as you say a class can be used as a noun, or as a verb, and the same can be said of many other structures/programming objects, like variables, like structs, like functions themselves, like arrays, dictionaries, etc. etc. It all comes down to what you and your team (or just you if it's just your project) understands better. Another way I like to think of classes when reading other people's code is, a class is a way of materializing a concept, not just concrete things (so in terms of nouns, it includes both concrete and abstract nouns). In that sense, sometimes classes are used for nouns, but not always concrete nouns, but abstract nouns too, like say for example a manager class, or a behavior class (enemy/player for a game, user for a web application for example, etc.). And sometimes, they are also used to split the responsibilities/traits that define a 'thing' (concrete or abstract) in your code. So user's responsibilities could be split into an user manager class, an user info class, an user behavior class, etc. etc., in this case, different classes refer to the same (user) but talk about different *aspects* of it, so it's used as both a noun and as a verb, but in different classes (the same class is not used as both verb and noun, well, ideally at least). Classes are used in a lot of ways, but if we stick to how oop was conceived back in its time, I feel the goal behind of classes is to group related data and concepts, in an attempt to make code more readable and flexible (although to be honest, I'd say just readable, I mean flexible to me is a fancy way of saying that the changes require more work, but you can achieve the same with other approaches too, I feel like the biggest criteria to go for one approach or another is always readability at the end of the day, but anyone can disagree with me there, that's ok). Long response, but I hope to have provided some insights! I used to be so confused about these topics and thinking this way has helped me a lot in that regard, so hopefully it helps you or someone else reading this thread too :) .
@MrTrebor2 Жыл бұрын
I strongly belive part II will cover right ways to decoupling states, adding new state without refactoring previous states, controlling possible transitions in one place.
@digiryde4 ай бұрын
The State pattern is one of my Gotos in ERP system usage.
@ЭльдарДевликамов-ю5р Жыл бұрын
That’s the content we deserve!
@ArjanCodes Жыл бұрын
Thank you for the kind words!
@quillaja Жыл бұрын
A game programming book was my first encounter with finite state machines in programming as well.
@killermouse0 Жыл бұрын
I could see it being used to model the flow of a state machine, for example when implementing a chatbot with different commands requiring different prompts and parameters, that sorts of things. Anyway, super interesting video, thanks!
@ArjanCodes Жыл бұрын
Glad you enjoyed the video!
@xxlarrytfvwxx9531 Жыл бұрын
Hey, I'm trying to add a state machine into a project of mine but I kept deleting the code. 17:48 I'm using pygame and I have yet to find anything in the docs referring to a state module.
@1kroolik2 Жыл бұрын
Is the forward reference the only reason to introduce DocumentContext? You can workaround it by wrapping the Document type-hint in quotes. It will work as good.
@xiggywiggs Жыл бұрын
glad to see a design pattern video again! the state pattern is fun to me specifically because years ago I helped my sister make a game for a python class she had to take. While I didn't know about patterns at the time, I knew enough about how to avoid ending up with spaghetti code and getting overwhelmed that we ended up "inventing" the state pattern organically as we built the game. When I learned about patterns later, having stumbled into one on my own previously helped me intuitively grasp the idea.
@hansjovis6634 Жыл бұрын
Does the state design pattern tend to break the interface segregation principle? Is there a way to avoid it? I mean that the state interface assumes that every state supports the same state transition, by forcing every state to implement the same transition methods. In the document example the transition from edit to final is not possible, for example.
@anikethdeshpande83368 ай бұрын
i think you were enjoying playing the game, because i enjoyed watching you play... its an interesting game 😄
@ArjanCodes8 ай бұрын
Thank you! It's a fun game :)
@ralex75110 ай бұрын
You are the best!!! 👏
@ArjanCodes10 ай бұрын
Thank you!
@hobe4576 Жыл бұрын
I really like your coding videos and the content of that specific one. However, I feel that the Python code looks pretty java-ish and I wonder if there are more Pythonic ways of reaching the same target. The first small thing coming to my mind is obviously setting the state directly or via property instead of writing a setter method. Second observation is that the separation of state is a huge overhead especially when handling a low number of states. Whenever the standalone state has no meaning without the context object, "flattening down" might be much more readable in my opinion. In the light bulb example this would result in implementation of two classes "DarkLightBulb" and "BrightLightBulb", each with a switch method returning an instance of the other class. This would separate the state transitions equally well in my opinion. Thirdly, in a more functional oriented setup the pupose of the state pattern could be reached by using the multidispatch package. In that case instances of the context class, sketched as @dataclass Context: state: State content: Whatever together with different instances of some multifunction "get_altered_context(state: ConcreteState, content: Whatever)->Context" would handle each of the state transitions in an equally separated and readable way. I assume this approach might be much cleaner when extending to multiple logical states that influence each other. Would be really cool if you could produce a follow-up video later discussing more Pythonic (?) alternatives to the state pattern as presented here (by the way: I really liked the videos where you did the same for the factory pattern).
@haldanesghost Жыл бұрын
Registry pattern next please!
@sverrenystad7495 Жыл бұрын
Great video!
@ArjanCodes Жыл бұрын
Glad you enjoyed it!
@AndreaDalseno4 ай бұрын
Very formative video, as usual (I don't know why, but I missed this one). The penguin game is really lovely ( I love penguins). Have you ever thought of making a mobile version of it? I think kids would enjoy it ( I don't say it will be the next Angry Birds, but it has some required reasoning that could be a good choice for the kids and the parents as well). By the way, confess, you were actually looking for pictures of naked penguins 😂
@ArjanCodes4 ай бұрын
Thank you Andrea, glad you enjoyed the video! I thought of making a mobile version of the game, but unfortunately at the moment I don't have the time (the YT channel is a lot of work). Perhaps when I'm retired :).
@issazaytoun8348 Жыл бұрын
how to do you make this crazy autocomplition(2;48). Can you please share your extension and the shortcuts you are using 🙂
@Han-ve8uh Жыл бұрын
1. 4:43 What does line 17 do? I thought Protocols only need to enforce behaviour (methods) and not what instance variables exist in classes that implement the protocol. 2. Can this whole pattern be taught without Protocols? the indirections were very confusing. 3. 6:07 what if class Document was moved on top, does that solve the problem? Does not using protocols at all solve the problem too General feedback: Super high cognitive load to trace to understand, then later keep track what is going on, which are protocols, dataclasses, and actual implementing classes. Would be very helpful to intro the call stack, then dive deep, then summarize what's optional/alternatives. Currently, the videos dive deep straightaway and leave the viewer spending significant effort tracing themselves. For example, line 41 self.document.content was an important step forward in understanding relationship between those classes
@Naej7 Жыл бұрын
My godness, that old-ass JavaScript 😂
@ArjanCodes Жыл бұрын
It’s been in my family for 3 generations 😁.
@dinoscheidt Жыл бұрын
Google Trends going up now on „Penguin Eggs Life“
@mikeofs1304 Жыл бұрын
I think, Document method set_state should be underscored (protected), because it giving to change state in any direction, ignore the graph of states of state machine implemented in states methods. .... document.set_state(Draft(document)) document.edit() document.show_content() >>> Document content : Edited content Edited content
@smenci Жыл бұрын
What are the three points after the function definitions?
@BorgPrincess Жыл бұрын
Python actually allows these three points as code saying: "I may eventually define this, right now you only need to know this function's signature." It _does_ pretty much the same as a "pass" would do, but it's somewhat more clear that there is something missing here, to be spelled out later or somewhere else - in this case, in another function of the same name effectively overriding the protocol function. Whether you use "..." or "pass" seems to be mainly a question of style, with the "..." arguably more suggestive but much newer and therefore not so widely used yet.
@runamucker Жыл бұрын
I've been using Pygame for a few years as a hobby, and I don't think there's any built-in feature that provides the state management. Am I wrong?
@agustinayala6438 Жыл бұрын
Excelent video! I found your channel looking for design patterns some time ago and it seemed to me very interesting. The only problem I had was that I was forced to research about other topics (e.g. API's, business concepts), and I had to watch the video over and over again to get the idea. When you use a simple example it's much easier to understand. You're a very good teacher but for some simple mortals like me it's difficult to follow you from time to time.
@izzikora6751 Жыл бұрын
One trick is to lower the playback speed to like 0.75x or 0.5 so it's easier to keep up
@agustinayala6438 Жыл бұрын
@@izzikora6751 Thanks for the advice, I always forget about that feature.
@izzikora6751 Жыл бұрын
@@agustinayala6438 anytime
@WalterVos Жыл бұрын
Shouldn't that set_state method have an underscore prefix? It seems like it should be "private" 😉
@LunarSoul255 Жыл бұрын
How are you able to access a state before it's defned? Like, in the lightbulb example, OnState sets its target bulb to OffState, but OnState is defined before OffState in the code chronologically. It's been my experience in Python as a whole that you can't access something before it's defined in the code like this. I'm also not entirely sure what the use of the Protocol class is here? The docs don't provide much help in that regard, either.
@aflous Жыл бұрын
Python doesn't need to know the complete definition of a class at the time it's first referenced. It only needs to know that the class exists. This is because Python functions are not evaluated until they are called, not when they are defined. When the `switch` method of `OnState` class references `OffState`, it doesn't need to know anything about `OffState` other than it's a class. The actual methods and properties of `OffState` are only necessary when the `switch` method is executed. At that point, the full definition of `OffState` must be completed, or you'll get a runtime error if you try to use it in a way that relies on parts of its definition that Python hasn't seen yet.
@akiidjk Жыл бұрын
A video on python multithreading?
@muzafferckay2609 Жыл бұрын
I am corious when you are going to cover repository design pattern deeply to solving all mentioned issues at internet
@meh48419 ай бұрын
The green penguin doesn’t have to be sacrificed!! 😢 Blues to the right, Browns to the green, Browns to the right, then make your pairs
@letsgobrandon1327 Жыл бұрын
I love your videos. Plus, you look like and sounds like Matt Walsh. Even your joke style... It's like watch Matt Walsh teaching Python.
@robberttruijens6552 Жыл бұрын
😂
@letsgobrandon1327 Жыл бұрын
Am I crazy, or he looks like a Matt Walsh European brother? @@robberttruijens6552
@nnii-rn7co8 ай бұрын
The State Design Pattern you describe here actually looks really much like the Strategy Design Pattern (kzbin.info/www/bejne/jYKbk4Garbd4frc). The difference is that the State Design Pattern takes the strategy from the "state" attribute. Thanks for the good videos!
@zbaktube Жыл бұрын
Just a quick thought: I can imagine using it with Hypermedia rest apis...
@Septumsempra88187 ай бұрын
I'm listening
@adamstrejcovsky8257 Жыл бұрын
You are OG
@ArjanCodes Жыл бұрын
Thank you for the kind words, Adam!
@cyclistArsenal Жыл бұрын
Giving me stateflow simulink vibes 😂😂. I have to use in automobile sw dev this shit😂
@miyu545 Жыл бұрын
Has to best programming videos on actually how to program rather than just telling someone to type.
@yickysan Жыл бұрын
I want to intern under you. 🥺
@alexd74669 ай бұрын
Please don't code like this in Python. this looks like Java. The really isn't any reason for using classes everywhere - it only makes things slow and unreadable. Remember that object-oriented is not the same as class-oriented.
@zbaktube Жыл бұрын
One thing is not clear to me. Today's people or the penguins are more opened nowadays? 🙂
@RoysIdea Жыл бұрын
Oh how I hate the sound/noise of a mechanical keyboard. I wouldn’t mind if they were banned