Skip to content

Commit 888a4eb

Browse files
committed
add State pattern
1 parent 9a5dcb6 commit 888a4eb

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

BehavioralPatterns/State/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# State Pattern
2+
3+
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
4+
5+
## Problem
6+
7+
- An object should change its behavior when its internal state changes.
8+
- State-specific behavior should be defined independently.
9+
- New states should be added
10+
- the behavior of existing states should be changed independently
11+
12+
## Solution
13+
14+
- Define separate State objects that encapsulate state-specific behavior for each state.
15+
- A class delegates state-specific behavior to its current state object instead of implementing state-specific behavior directly.
16+
17+
## Common Structure
18+
19+
![Common structure of State pattern](img/structure.jpg)
20+
21+
* Context
22+
* maintains an instance of ConcreteState that defines the current state.
23+
* State
24+
* defines an interface for encapsulating the behavior associated with a particular state of the Context.
25+
* ConcreteState
26+
* each subclass implements a behavior associated with a state of the Context.
27+
28+
## Collaboration
29+
30+
* Context delegates state-specific requests to the current ConcreteState object.
31+
* A context may pass itself as an argument to the State object handling the request. This lets the State object access the context if necessary.
32+
* Context is the primary interface for clients. Clients can configure a context with State objects. Once a context is configured, its clients don't have to deal with the State objects directly.
33+
* Either Context or the ConcreteState subclasses can decide which state succeeds another and under what circumstances.
34+
35+
## Benefits
36+
37+
* Organizes the code related to particular states into separate classes.
38+
* It makes state transitions explicit by using separate objects for different states.
39+
* Simplifies the code of the context
40+
* State objects can be shared, if they have no instance variables (cf. Flyweight with no intrinsic state but only behavior)
41+
42+
## Drawbacks
43+
44+
* Can be overkill if a state machine has only a few states or rarely changes.
45+
46+
## Example
47+
48+
**Definition**
49+
50+
**Usage**
51+
52+
## Comparison with other patterns
53+
54+
* **Flyweight** pattern explains when and how State objects can be shared.
41.3 KB
Loading

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Design Patterns in C# / .NET
4040
|:heavy_check_mark:| [Memento](/BehavioralPatterns/Memento)|
4141
| :heavy_check_mark: | [Null Object](/BehavioralPatterns/NullObject) |
4242
| :heavy_check_mark:| [Observer](/BehavioralPatterns/Observer/) |
43-
| | State | :warning:
43+
| | [State](/BehavioralPatterns/State)
4444
|:heavy_check_mark: | [Strategy](/BehavioralPatterns/Strategy/) |
4545
|:heavy_check_mark: | [Template Method](/BehavioralPatterns/TemplateMethod) |
4646
| | Visitor

0 commit comments

Comments
 (0)