mercredi 3 juillet 2019

OOP Design Patterns: What are the advantages of the state pattern?

I am learning about design patterns and I have learnt about the State and the Strategy pattern.

The Strategy pattern focuses on using different implementations to achieve the same task, whereas the State pattern centres around performing different tasks based on the state of the object.

When would using the state pattern be more advantageous than using the strategy pattern? Also, what would be the drawbacks of the state pattern when compared to the strategy pattern?

With regards to the state pattern, I think it would be better to use it in scenarios when actions can be performed on objects that result in their state changing, with the state of the object dictating its behaviour.

However, I am not sure when one would use the strategy pattern over the state pattern. I think that one would choose the strategy pattern if when actions are performed on the objects, they don't change state. Different types of a set of objects may have to employ different methods to achieve the same task, and this can be achieved through using the strategy pattern.

Are there any other disadvantages/advantages each of these patterns have when compared to the other? Any insights are appreciated.

Aucun commentaire:

Enregistrer un commentaire