From the course: Design Patterns in Go for Object Oriented Programming

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

State pattern

State pattern

- [Instructor] Let's talk about the state pattern. The state design pattern works as an object-oriented state machine. It allows objects to alter their behavior depending on their internal state, which ultimately allows the object to appear to have changed its class. You would want to use the state pattern if you have an object that behaves differently depending on the state of that object and you have a large number of different states. You could also use the state pattern if you see within your class that you have a bunch of conditionals that are used to represent different states. The state pattern is also useful when you want to reduce the amount of duplicate code that you may have across different classes. Here's how the state design pattern is structured. The state interface declares the methods that are being used for all of the different states. The concrete states implement the state methods and provide their…

Contents