4 ms·
There is nothing "object oriented" about the example, except for the fact that the code is in a class. The "proper object oriented way" would be the State desi
by userbinator 13y ago
There is nothing "object oriented" about the example, except for the fact that the code is in a class.
The "proper object oriented way" would be the State design pattern, using separate classes for each state and replacing the if/else with a virtual call, which I agree would be even more obfuscation of the control flow:
http://en.wikipedia.org/wiki/State_pattern http://en.wikipedia.org/wiki/State_pattern
No, I don't think the answer is to make yet another library, if it isn't one that encodes the state graph as a table.
- munro 13y agoYep! I completely agree, there is nothing object oriented about the example. So why use constructs meant for describing objected oriented code? Though, the fact that someone defined a pattern for designing state machines on top of object oriented constructs is interesting, thanks for bringing that up. The other option is build a model for talking about state machines on top of Python's features (like decorators, meta classes, even coroutines!) than strict OOP to feel more like idiomatic Python, than say Java. And when someone has written the code already, I say why reinvent the wheel! :)
- Too 13y agoClasses are a way to encapsulate state, you don't have to use them together with oo. How else would you describe the example in python without using object oriented constructs? Global variables or passing around a dictionary with all the things set up in init?
- munro 13y agoA distraction from the main point, being: state graphs rock! And deserve a high level way of talking about them in any language of choice, which most languages don't include in the stdlib. So one may have to roll their own/find a library. And that the article confused code that's "difficult to understand" with state graphs being bad. > Classes are a way to encapsulate state, you don't have to use them together with oo. Lexical closures are another way to encapsulate state, modules are another, then there are monads! People have thought of lots of cool ways to encapsulate state. > How else would you describe the example in python without using object oriented constructs? Global variables or passing around a dictionary with all the things set up in init? I would build the example on state graph terminology, instead of on top of Python's class directly. Though, as userbinator pointed out, someone has described an OOP pattern for talking about state graphs [1]. Unfortunately there is no built in way for talking about state graphs in Python. To build that, Python has lots of fun features I can leverage! I wouldn't artificially limit myself. But first I would definitely research if someone has written a good library. [1] http://en.wikipedia.org/wiki/State_pattern http://en.wikipedia.org/wiki/State_pattern