Subscribe to PHP Freaks RSS

StarTutorial.com: Understanding Design Patterns - State

syndicated from www.phpdeveloper.org on July 9, 2018

StarTutorial.com has posted a new tutorial in their series introducing commonly used design patterns. In their latest article they cover the State pattern.

[The State pattern] allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

In the example situation, a worker at a customer support company and his state when dealing with customers (happy/angry/moderate). They codify this in a SupportRep class with a "state" value to track his mood. They expand on this with some additional conditions for number of calls to change the state as time goes on. This requires several if/else statements to be added. They refactor this to reduce the clutter using the State pattern, passing off the logic to various "state" classes.