mercredi 4 novembre 2015

Chain of responsibility vs Finite State Machine - differences

So as I am a bit of electrician and programmer I thought I knew FSM design pattern very well. It is:

  • We have set of Nodes,
  • Each Node knows, what to do, when program is in this node,
  • Each Node contains references to another chosen nodes, and knows under what condition, should he proceed to the chosen one.
  • On event or after processing a Node, Node proceeds to the next chosen Node

I thought, that it was quite clear to me. Although recently, when I was implementing a State Machine one person told me, that it is in fact a bit modified Chain of responsibility (not sure if he was correct) and , what I did/had was:

  • Set of Nodes (which did not represented a linear or tree structure)
  • Nodes had objects, that knew under which condition they should jump to which Node
  • Each Node had it's own context of processing (some parts of contexts were shared between Nodes).

Unfortunatelly I am afraid, that due to legal issues I am not allowed to paste a class diagram here.


On the other hand we have got chain of responsibility, which I would (as I understand) define in a following way, that is:

  • We have got some ItemToProcess Interface,
  • We have got some Node Interface,
  • Node has a reference to only one next Node,
  • Each Node processes ItemToProcess and forwards processed one to the nextNode

So as far as I understand:

  • We use Chain Of Responsibility, where we want One item to be processed (or at least tried to be processed) by each node
  • Chain of responsibility represents sequential and constant execution of processes
  • We use StateMachine to represent graphs
  • We use StateMachine to perform computations, which order or kinds of computations may vary depending on some events.

I would like to ask you to confirm my understanding of those design patterns or tell me where I am making mistake in understanding.

Aucun commentaire:

Enregistrer un commentaire