mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-06-05 02:21:34 +00:00
docs: Content SEO updates (#2990)
* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
This commit is contained in:
+16
-14
@@ -1,22 +1,24 @@
|
||||
---
|
||||
title: State
|
||||
title: "State Pattern in Java: Enhancing Behavior Dynamics with State Encapsulation"
|
||||
shortTitle: State
|
||||
description: "Explore the State Pattern, a core component of Java design patterns that enables dynamic behavior change in objects with internal state shifts. Includes real-world examples, applicability, benefits, and detailed code snippets."
|
||||
category: Behavioral
|
||||
language: en
|
||||
tag:
|
||||
- Decoupling
|
||||
- Gang of Four
|
||||
- State tracking
|
||||
- Decoupling
|
||||
- Gang of Four
|
||||
- State tracking
|
||||
---
|
||||
|
||||
## Also known as
|
||||
|
||||
* Objects for States
|
||||
|
||||
## Intent
|
||||
## Intent of State Design Pattern
|
||||
|
||||
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
|
||||
Enable an object to alter its behavior dynamically as its internal state changes, optimizing Java application responsiveness.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of State Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
@@ -36,7 +38,7 @@ Wikipedia says
|
||||
|
||||
> The state pattern is a behavioral software design pattern that allows an object to alter its behavior when its internal state changes. This pattern is close to the concept of finite-state machines. The state pattern can be interpreted as a strategy pattern, which is able to switch a strategy through invocations of methods defined in the pattern's interface.
|
||||
|
||||
**Programmatic Example**
|
||||
## Programmatic Example of State Pattern in Java
|
||||
|
||||
In our programmatic example there is a mammoth with alternating moods.
|
||||
|
||||
@@ -153,36 +155,36 @@ Program output:
|
||||
The mammoth is calm and peaceful.
|
||||
```
|
||||
|
||||
## Applicability
|
||||
## When to Use the State Pattern in Java
|
||||
|
||||
* An object's behavior depends on its state, and it must change its behavior at runtime depending on that state.
|
||||
* Operations have large, multipart conditional statements that depend on the object's state.
|
||||
|
||||
## Known Uses
|
||||
## Real-World Applications of State Pattern in Java
|
||||
|
||||
* `java.util.Iterator` in Java's Collections Framework uses different states for iteration.
|
||||
* TCP connection classes in network programming often implement states like `Established`, `Listen`, and `Closed`.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of State Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
* Localizes state-specific behavior and partitions behavior for different states.
|
||||
* Makes state transitions explicit.
|
||||
* State objects can be shared among different contexts.
|
||||
* Reusable State objects can be efficiently shared among various contexts in Java, enhancing memory management and performance.
|
||||
|
||||
Trade-offs:
|
||||
|
||||
* Can result in a large number of classes for states.
|
||||
* Context class can become complicated with the state transition logic.
|
||||
|
||||
## Related Patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Flyweight](https://java-design-patterns.com/patterns/flyweight/): State objects may be shared between different contexts.
|
||||
* [Singleton](https://java-design-patterns.com/patterns/singleton/): State objects are often singletons.
|
||||
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Both patterns have similar structures, but the State pattern's implementations depend on the context’s state.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)
|
||||
|
||||
Reference in New Issue
Block a user