Update README.md

This commit is contained in:
Ilkka Seppälä 2020-09-13 18:09:18 +03:00
parent 9c648cbdb8
commit 9cbc509c3a
4 changed files with 24 additions and 89 deletions

View File

@ -9,15 +9,21 @@ tags:
---
## Also known as
Objects for States
## Intent
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Allow an object to alter its behavior when its internal state changes. The object will appear to
change its class.
## Explanation
Real world example
> When observing a mammoth in its natural habitat it seems to change its behavior based on the situation. It may first appear calm but over time when it detects a threat it gets angry and dangerous to its surroundings.
> When observing a mammoth in its natural habitat it seems to change its behavior based on the
> situation. It may first appear calm but over time when it detects a threat it gets angry and
> dangerous to its surroundings.
In plain words
@ -25,7 +31,10 @@ In plain words
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.
> 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**
@ -126,17 +135,23 @@ And here is the full example how the mammoth behaves over time.
mammoth.observe();
mammoth.timePasses();
mammoth.observe();
// The mammoth gets angry!
// The mammoth is furious!
// The mammoth calms down.
// The mammoth is calm and peaceful.
```
Program output:
```java
The mammoth gets angry!
The mammoth is furious!
The mammoth calms down.
The mammoth is calm and peaceful.
```
## Class diagram
![alt text](./etc/state_1.png "State")
![alt text](./etc/state_urm.png "State")
## Applicability
Use the State pattern in either of the following cases
* An object's behavior depends on its state, and it must change its behavior at run-time depending on that state

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

View File

@ -1,80 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
<class id="1" language="java" name="com.iluwatar.state.AngryState" project="state"
file="/state/src/main/java/com/iluwatar/state/AngryState.java" binary="false" corner="BOTTOM_RIGHT">
<position height="124" width="157" x="98" y="310"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.state.PeacefulState" project="state"
file="/state/src/main/java/com/iluwatar/state/PeacefulState.java" binary="false" corner="BOTTOM_RIGHT">
<position height="124" width="173" x="295" y="310"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<interface id="3" language="java" name="com.iluwatar.state.State" project="state"
file="/state/src/main/java/com/iluwatar/state/State.java" binary="false" corner="BOTTOM_RIGHT">
<position height="106" width="136" x="138" y="674"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="4" language="java" name="com.iluwatar.state.Mammoth" project="state"
file="/state/src/main/java/com/iluwatar/state/Mammoth.java" binary="false" corner="BOTTOM_RIGHT">
<position height="160" width="177" x="138" y="474"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<realization id="5">
<bendpoint x="107" y="474"/>
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="3"/>
</realization>
<association id="6">
<end type="SOURCE" refId="4" navigable="false">
<attribute id="7" name="state"/>
<multiplicity id="8" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="3" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="9">
<bendpoint x="345" y="474"/>
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="3"/>
</realization>
<association id="10">
<end type="SOURCE" refId="1" navigable="false">
<attribute id="11" name="mammoth"/>
<multiplicity id="12" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="4" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="13">
<end type="SOURCE" refId="2" navigable="false">
<attribute id="14" name="mammoth"/>
<multiplicity id="15" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="4" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB