# State Pattern

> A design pattern that gives each state of an object its own class, so behavior and legal transitions change with the state.

Record: `architecture:state-pattern`
Kind: pattern
Layer: [Design Patterns Core](https://banes-lab.com/records/layer/design-patterns-core.md)
Severity: recommended
Scope: behavior, state machine, lifecycle
Canonical: https://banes-lab.com/ontology#architecture-state-pattern

Listed in [Architecture principles](https://banes-lab.com/api/records/architecture.md), after [Command Pattern](https://banes-lab.com/records/architecture/command-pattern.md) and before [Chain of Responsibility Pattern](https://banes-lab.com/records/architecture/chain-of-responsibility-pattern.md).

## Repair

- Refactored by: Replace State-Conditional with State objects
- Detected by: repeated conditionals on a status field
- Violated by: behavior branched on scattered status flags
- Measured by: status-conditional density
- Enforced by: [design review](https://banes-lab.com/records/architecture/design-review.md)

## Requires

- [Explicit State Model](https://banes-lab.com/records/lexicon/explicit-state-model.md)

## Reinforces

- [Open/Closed Principle (OCP)](https://banes-lab.com/records/architecture/open-closed.md)
- [Polymorphism](https://banes-lab.com/records/architecture/polymorphism.md)

## Enables

- [State-Local Behavior](https://banes-lab.com/records/lexicon/state-local-behavior.md)
- [Legal-Transition Enforcement](https://banes-lab.com/records/lexicon/legal-transition-enforcement.md)

## Conflicts with

- [Boolean Flag Soup](https://banes-lab.com/records/lexicon/boolean-flag-soup.md)

## In tension with

- [Class Proliferation](https://banes-lab.com/records/lexicon/class-proliferation.md)

## Tensions

- [State Pattern / Class Proliferation](https://banes-lab.com/records/tension/class-proliferation-state-pattern.md)

## Contracts

- [State Pattern](https://banes-lab.com/records/algorithms/state-pattern.md)

## Severity

- [recommended](https://banes-lab.com/records/vocabulary/severity-recommended.md)

## Category

- [Behavioral Patterns](https://banes-lab.com/records/architecture-category/behavioral-patterns.md)

## Reinforced by

- [Finite State Machine](https://banes-lab.com/records/architecture/finite-state-machine.md)

## Linked from

- [SOLID / Object-Oriented Design](https://banes-lab.com/ontology/principles/architecture-category-solid-object-oriented-design.md)
- [Behavioral Patterns](https://banes-lab.com/ontology/principles/architecture-category-behavioral-patterns.md)
- [Behavioral Patterns](https://banes-lab.com/ontology/lexicon/lexicon-category-behavioral-patterns.md)
- [Core Vocabulary](https://banes-lab.com/ontology/lexicon/lexicon-category-core-vocabulary.md)
- [Architectural Clusters](https://banes-lab.com/ontology/algorithms/algorithms-domain-architectural-clusters.md)
- [Severity levels](https://banes-lab.com/ontology/schema/the-vocabulary-severity.md)
- [The resolutions](https://banes-lab.com/ontology/schema/the-resolutions.md)
