So as I am a bit of electrician and programmer I thought I knew FSM design pattern very well. It is:
Nodes
,Node
knows, what to do, when program is in this node,Node
contains references to another chosen nodes
, and knows under what condition, should he proceed to the chosen one. event
or after processing
a Node, Node proceeds
to the next chosen NodeI 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:
Nodes
(which did not represented a linear or tree structure)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:
ItemToProcess
Interface,Node
Interface,ItemToProcess
and forwards processed one to the nextNode
So as far as I understand:
Chain Of Responsibility
, where we want One item to be processed (or at least tried to be processed) by each nodeStateMachine
to represent graphsStateMachine
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.
State design pattern is used to define and manage state of an object, while Strategy pattern is used to define a set of interchangeable algorithm and lets client to choose one of them. So Strategy pattern is a client driven pattern while Object can manage there state itself.
One can now clearly see the difference between Strategy and State pattern, their intent is different. State pattern helps object to manage state, while Strategy pattern allows the client to choose different behavior. Another difference, which is not easily visible is, who drives change in behavior.
This chapter presents an FSM pattern language that addresses several recurring design problems in implementing a state machine in an object-oriented design. The pattern language includes a basic design pattern for FSMs whose design evolves from the general understanding of state machines functionality.
Decorator Pattern says wrap an original object and add additional features in the wrapper object. So structurally speaking - Wrappers follow decorator pattern. Adapter pattern says changing one object by creating an instance of it and adding functionalities to it.
I'll complement the other answer by saying that design patterns also consider making software easily extendable.
Chain of responsibility has the advantage of being able to code new ConcreteHandler
classes to extend your processing functionalities, without the Client
class having to be modified.
The code the builds the chain, however, must be modified to add the new handler as an object:
State is not as flexible if you want to add new concrete states. The GoF book shows this diagram:
What's not obvious (read more in this answer) is that Handle()
events are coupled to another ConcreteState
class (i.e., the next state). So, coding a new ConcreteState
could require changes in some or all of the existing ConcreteState
classes.
It's perhaps not as easy to add new states in the State pattern as it is to add new handlers in the Chain of Responsibility pattern.
Your understanding is correct.
I would add that the Nodes in a FSM are different states. When you switch to a different node, you change state. You may call the same state/node several consecutive times.
The chain of responsibility does not have different states. As you said, each node in the chain tries to process an object and if a node successfully processes the object, then usually the chain halts.
Common uses for chain of responsiblity are lookups for figuring out what Handler to use for a given input such as file types or extensions or finding items in a classpath or resource locator. You can think of those types of operations as :
[Node 1]
"-Do you know what this is?"
-No
[Node 2]
"-Do you know what this is?"
-No
[Node 3]
"-Do you know what this is?"
-Yes!
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With