© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
V. SarcarJava Design Patternshttps://doi.org/10.1007/978-1-4842-7971-7_22

22. State Pattern

Vaskaran Sarcar1  
(1)
Garia, Kolkata, India
 

This chapter covers the State pattern.

GoF Definition

It allows an object to alter its behavior when its internal state changes. The object will appear to change its class.

Concept

The GoF definition is easy to understand. It states that an object can change what it does and this depends on its current state.

Suppose you are dealing with a large-scale application where the codebase is rapidly growing. As a result, the situation becomes complex and you may need to introduce lots of if-else blocks/switch statements to guard the various ...

Get Java Design Patterns: A Hands-On Experience with Real-World Examples now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.