如果我只有几个状态,状态模式可能有帮助吗?

发布于 2024-10-04 16:33:53 字数 1432 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌梦深 2024-10-11 16:33:53

不,这并不过分。即使您的实际状态很少并且预计不会添加更多状态,使用状态模式也会使您的系统更易于测试,因为您可以单独测试各个状态。

No, it's not overkill. Even if you have few actual states and don't anticipate adding more, using the State pattern will make your system easier to test as you can test individual states in isolation.

明月松间行 2024-10-11 16:33:53

对于您的具体情况,我们不能说太多,因为您提供的信息太少。

但总的来说...


其他表达的意见可能有所不同,但是...

敏捷运动正在取得进展很大的力量。
他们会推荐YAGNI你不需要它)。

这个想法是将复杂性推迟到以后,因为:

  1. 我们开发人员往往喜欢复杂性,有时将其放在不需要的地方。
  2. 稍后你会更好地理解你的代码;然后你可能会找到一个更简单的解决方案。
  3. 您的用户(或指定要做什么的人,即使是您)稍后将使用您的软件,因此需求可能会发生变化。因此,任何预期的复杂性都可能在那时被证明是不可用的。
  4. 即使在(罕见的)情况下,您稍后会添加与现在添加完全相同的复杂性......同时,您的代码库更简单,因此您的其他编码活动更简单、更快。这也是一种收获。
  5. 还要考虑到您的时间不是无限的,并且可能有人为此付费(即使是您,如果这是您的空闲时间或培训时间)。如果你把时间花在当前的问题上,而不是花时间在未来上,你的努力可能会更有用……

也就是说,如果你喜欢这个想法,那就去做吧,追随你的梦想! :-)

We can't say much in your precise case, because your give too little information.

But in general ...


Other expressed opinions may differ, but ...

The Agile movement is gaining a lot of strengh.
They would recommend YAGNI (You Aren't Gonna Need It).

The idea is to delay complexity until later, because :

  1. We developpers tend to love complexity, and sometimes put it where it is not needed.
  2. You will understand your code better later ; and then you might find a simpler solution.
  3. You user (or the person who specifies what to do, even if that is you) will have used your software later, so the requirements are likely to change. So any complexity anticipated might prove unusable at that point.
  4. Even in the (rare) case that you would later add exactly the same complexity that you would add now ... In the mean time, your code base is simpler, so your other coding activities are simpler and faster. That is also a gain.
  5. Also consider that your time is not infinite, and possibly someone pays for this (even you if it is your free time, or training time). Your effort might be more useful if spend on the current problem, rather that spending time for the future...

That said, if you enjoy the idea, go for it, follow your dreams ! :-)

ζ澈沫 2024-10-11 16:33:53

你在谈论状态图吗?

无论如何,我想说,去吧。问题是将来您可能想要添加一个状态,然后认为不值得实现您的模式,因为它只是多了一个状态,对吧?然后你就有了 4 个状态,你必须添加另一个状态,然后重复这个过程。

然后你就进入了一种状态(双关语),你无法重构并使用状态模式,因为这会涉及重写太多代码。

所以,第一次就把事情做好。

are you talking about a state-chart?

Regardless, Id say go for it. The problem is in the future you might want to add a state, and then think its not worthwhile to implement your pattern because its just one more state, right? And then you've got 4 states, and you have to add another, and the process repeats.

And then you get to a state (pun-intended) where you can't refactor and use the states pattern because it would involve rewriting too much code.

So, do it right the first time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文