mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-14 16:17:25 +00:00
docs: update method
This commit is contained in:
1 parent
790d2d6916
commit
723b1bf311
1 file changed
+56
-15
+56
-15
@@ -49,7 +49,7 @@ public class World {
|
|||||||
entities = new ArrayList<>();
|
entities = new ArrayList<>();
|
||||||
isRunning = false;
|
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.
|
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
|
```java
|
||||||
var world = new World();
|
@Slf4j
|
||||||
var skeleton1 = new Skeleton(1, 10);
|
public class App {
|
||||||
var skeleton2 = new Skeleton(2, 70);
|
|
||||||
var statue = new Statue(3, 20);
|
private static final int GAME_RUNNING_TIME = 2000;
|
||||||
world.addEntity(skeleton1);
|
|
||||||
world.addEntity(skeleton2);
|
public static void main(String[] args) {
|
||||||
world.addEntity(statue);
|
try {
|
||||||
world.run();
|
var world = new World();
|
||||||
Thread.sleep(GAME_RUNNING_TIME);
|
var skeleton1 = new Skeleton(1, 10);
|
||||||
world.stop();
|
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.
|
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
|
## Applicability
|
||||||
|
|
||||||
Update Method works well when:
|
Update Method works well when:
|
||||||
|
|||||||
Reference in new issue
Block a user