The state pattern
The state pattern is used in situations where an object behaves differently depending on its internal state. A networking service is a great example. A typical implementation for a network-based service is to listen to a specific port number. When a remote process connects to the service, it establishes a connection, and they use it to communicate with each other until the end of the session. When a network service is currently in a listening state, it should allow a new connection to be opened; however, no data transmission should be allowed until after the connection is opened. Then, after the connection is opened, we should be able to send data. By contrast, we should not allow any data to be sent through the network ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access