The last pattern in this chapter, state, allows an object to change its behavior on demand. This is especially useful when an object implements an algorithm that goes through different execution states. If the object's internal behavior changes when its state is changed, you've got an excellent candidate for a state pattern.
Any vending machine follows the state pattern. The behavior of a machine when the customer presses the buttons to select a certain product depends on the current state. If the customer has already paid for the product, the machine will deliver the merchandise. Otherwise, it will only display the cost of the product.
This Gang of Four pattern can only be used in rare occasions. A typical candidate for the introduction of this pattern is an object that implements some kind of state machine. Standard examples include TCP socket management, line...