mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 10:58:42 +00:00
docs: update method
This commit is contained in:
+56
-15
@@ -49,7 +49,7 @@ public class World {
|
||||
entities = new ArrayList<>();
|
||||
isRunning = false;
|
||||
}
|
||||
// ...
|
||||
// Other properties and methods...
|
||||
}
|
||||
```
|
||||
|
||||
@@ -124,24 +124,65 @@ public void addEntity(Entity entity) {
|
||||
In the `App` class, we can see how the `World` class and its methods are used to create a game world, add entities to it, and start the game loop.
|
||||
|
||||
```java
|
||||
var world = new World();
|
||||
var skeleton1 = new Skeleton(1, 10);
|
||||
var skeleton2 = new Skeleton(2, 70);
|
||||
var statue = new Statue(3, 20);
|
||||
world.addEntity(skeleton1);
|
||||
world.addEntity(skeleton2);
|
||||
world.addEntity(statue);
|
||||
world.run();
|
||||
Thread.sleep(GAME_RUNNING_TIME);
|
||||
world.stop();
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
private static final int GAME_RUNNING_TIME = 2000;
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
var world = new World();
|
||||
var skeleton1 = new Skeleton(1, 10);
|
||||
var skeleton2 = new Skeleton(2, 70);
|
||||
var statue = new Statue(3, 20);
|
||||
world.addEntity(skeleton1);
|
||||
world.addEntity(skeleton2);
|
||||
world.addEntity(statue);
|
||||
world.run();
|
||||
Thread.sleep(GAME_RUNNING_TIME);
|
||||
world.stop();
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Console output:
|
||||
|
||||
```
|
||||
14:46:33.181 [main] INFO com.iluwatar.updatemethod.World -- Start game.
|
||||
14:46:33.280 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 11.
|
||||
14:46:33.281 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 71.
|
||||
14:46:33.452 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 12.
|
||||
14:46:33.452 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 72.
|
||||
14:46:33.621 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 13.
|
||||
14:46:33.621 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 73.
|
||||
14:46:33.793 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 14.
|
||||
14:46:33.793 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 74.
|
||||
14:46:33.885 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 15.
|
||||
14:46:33.885 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 75.
|
||||
14:46:34.113 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 16.
|
||||
14:46:34.113 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 76.
|
||||
14:46:34.324 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 17.
|
||||
14:46:34.324 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 77.
|
||||
14:46:34.574 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 18.
|
||||
14:46:34.575 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 78.
|
||||
14:46:34.730 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 19.
|
||||
14:46:34.731 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 79.
|
||||
14:46:34.803 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 20.
|
||||
14:46:34.803 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 80.
|
||||
14:46:34.979 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 21.
|
||||
14:46:34.979 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 81.
|
||||
14:46:35.045 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 22.
|
||||
14:46:35.046 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 82.
|
||||
14:46:35.187 [main] INFO com.iluwatar.updatemethod.World -- Stop game.
|
||||
14:46:35.288 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 1 is on position 23.
|
||||
14:46:35.289 [Thread-0] INFO com.iluwatar.updatemethod.Skeleton -- Skeleton 2 is on position 83.
|
||||
```
|
||||
|
||||
This is a basic implementation of the Update Method pattern. In a real-world application, the `Entity` class would likely have additional methods and properties, and the `update` method would contain more complex logic to simulate the entity's behavior.
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
|
||||
Update Method works well when:
|
||||
|
||||
Reference in New Issue
Block a user