有限状态机模式 - 一个真正的模式? [英] Finite State Machine Pattern - The One True Pattern?

查看:155
本文介绍了有限状态机模式 - 一个真正的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有的代码都可以通过应用状态机模式来改进?



我正在开发一个可怕的,可怕的,错误的,破碎的意大利面码。
我从这个博客复制了 Martin Fowler的示例状态机器代码并将整堆废物转化为一系列陈述。
从字面上看,只是一个国家,事件,过渡和命令的列表。



我不敢相信转型。代码现在很干净,可以工作。我当然知道国家机器之前,甚至实施了
,但在Martin Fowler的例子中,模型/配置的分离是惊人的。



我认为,几乎我所做的一切都可以从某种方式受益。我想使用我使用的每种语言的这个功能。
也许这应该是一个语言级别的功能。



任何人认为这是错误的?
或者任何人都有与其他模式类似的经验?

解决方案

有限状态机(FSM)通过以专门的语言描述解决方案,特定语言(DSL)可以更容易地将问题与一个特定的解决方案域进行匹配。



State Machine模式的局限性在于它本身构成一种编程语言,但是您必须编写自己的执行,测试和调试工具;和任何维护者必须学习的。您将代码的复杂性转移到复杂的FSM配置中。有时候,这是有用的,但肯定不是普遍的。



由于任何冯·诺依曼计算机本身就是一个FSM,那么肯定任何程序可以以这种方式重铸。


Could all Code ever written be improved by applying the State Machine Pattern?

I was working on a project that was a mass of horrendous awful, buggy, broken spaghetti code. I copied Martin Fowler's example State Machine code from this blog and transformed the whole heap of crap into a series of statements. Literally just a list of States, Events, Transitions and Commands.

I can't believe the transformation. The code is now clean, and works. Of course i was aware of State Machines before and have even implemented them but in the Martin Fowler example the separation of model/configuration is amazing.

This makes me think that almost everything i've ever done could have benefitted in some way from this approach. I want this functionality in every language i use. Maybe this should even be a language level feature.

Anyone think this is wrong? Or anyone have a similar experience with a different pattern?

解决方案

Finite state machines (FSM's) and more specifically domain specific languages (DSL's) make it easier to match a problem to one specific solution domain, by describing the solution in a specialised language.

The limitations of the State Machine pattern is that it itself constitutes a programming language, but one for which you have to write your own execution, testing and debugging tools; and one which any maintainer has to learn. You have moved the complexity of your code into a complex FSM configuration. Occasionally, this is useful, but certainly not universally.

And since any von Neumann computer is itself a FSM, then certainly any program can be recast in this fashion.

这篇关于有限状态机模式 - 一个真正的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆