mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-14 10:18:39 +00:00
docs: update state
This commit is contained in:
1 parent
07d56c77cf
commit
f7f318ad37
2 files changed
+7
-5
No files matched your search
+7
-4
@@ -49,7 +49,9 @@ public interface State {
|
|||||||
|
|
||||||
void observe();
|
void observe();
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class PeacefulState implements State {
|
public class PeacefulState implements State {
|
||||||
|
|
||||||
@@ -69,7 +71,9 @@ public class PeacefulState implements State {
|
|||||||
LOGGER.info("{} calms down.", mammoth);
|
LOGGER.info("{} calms down.", mammoth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class AngryState implements State {
|
public class AngryState implements State {
|
||||||
|
|
||||||
@@ -129,12 +133,15 @@ public class Mammoth {
|
|||||||
Here is the full example of how the mammoth behaves over time.
|
Here is the full example of how the mammoth behaves over time.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
var mammoth = new Mammoth();
|
var mammoth = new Mammoth();
|
||||||
mammoth.observe();
|
mammoth.observe();
|
||||||
mammoth.timePasses();
|
mammoth.timePasses();
|
||||||
mammoth.observe();
|
mammoth.observe();
|
||||||
mammoth.timePasses();
|
mammoth.timePasses();
|
||||||
mammoth.observe();
|
mammoth.observe();
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Program output:
|
Program output:
|
||||||
@@ -146,10 +153,6 @@ Program output:
|
|||||||
The mammoth is calm and peaceful.
|
The mammoth is calm and peaceful.
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
* An object's behavior depends on its state, and it must change its behavior at runtime depending on that state.
|
* An object's behavior depends on its state, and it must change its behavior at runtime depending on that state.
|
||||||
|
|||||||
@@ -46,6 +46,5 @@ public class App {
|
|||||||
mammoth.observe();
|
mammoth.observe();
|
||||||
mammoth.timePasses();
|
mammoth.timePasses();
|
||||||
mammoth.observe();
|
mammoth.observe();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user