Skip to content

Commit 55769e9

Browse files
Azureyjtiluwatar
authored andcommitted
Update Method pattern (iluwatar#1106)
* Add update method pattern * Add unit tests * Add README.md * Resolve conflict
1 parent 05e582c commit 55769e9

File tree

12 files changed

+1167
-477
lines changed

12 files changed

+1167
-477
lines changed

pom.xml

+478-477
Large diffs are not rendered by default.

update-method/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
layout: pattern
3+
title: Update Method
4+
folder: update-method
5+
permalink: /patterns/update-method/
6+
categories: Other
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
---
11+
12+
## Intent
13+
Update method pattern simulates a collection of independent objects by telling each to process one frame of behavior at a time.
14+
15+
## Applicability
16+
If the Game Loop pattern is the best thing since sliced bread, then the Update Method pattern is its butter. A wide swath of games featuring live entities that the player interacts with use this pattern in some form or other. If the game has space marines, dragons, Martians, ghosts, or athletes, there’s a good chance it uses this pattern.
17+
18+
However, if the game is more abstract and the moving pieces are less like living actors and more like pieces on a chessboard, this pattern is often a poor fit. In a game like chess, you don’t need to simulate all of the pieces concurrently, and you probably don’t need to tell the pawns to update themselves every frame.
19+
20+
Update methods work well when:
21+
22+
- Your game has a number of objects or systems that need to run simultaneously.
23+
24+
- Each object’s behavior is mostly independent of the others.
25+
26+
- The objects need to be simulated over time.
27+
28+
## Explanation
29+
The game world maintains a collection of objects. Each object implements an update method that simulates one frame of the object’s behavior. Each frame, the game updates every object in the collection.
30+
31+
To learn more about how the game loop runs and when the update methods are invoked, please refer to Game Loop Pattern.
32+
33+
## Credits
34+
35+
* [Game Programming Patterns - Update Method](http://gameprogrammingpatterns.com/update-method.html)

update-method/pom.xml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ The MIT License
4+
~ Copyright © 2014-2019 Ilkka Seppälä
5+
~
6+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
7+
~ of this software and associated documentation files (the "Software"), to deal
8+
~ in the Software without restriction, including without limitation the rights
9+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
~ copies of the Software, and to permit persons to whom the Software is
11+
~ furnished to do so, subject to the following conditions:
12+
~
13+
~ The above copyright notice and this permission notice shall be included in
14+
~ all copies or substantial portions of the Software.
15+
~
16+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
~ THE SOFTWARE.
23+
-->
24+
25+
<project xmlns="http://maven.apache.org/POM/4.0.0"
26+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
28+
<parent>
29+
<artifactId>java-design-patterns</artifactId>
30+
<groupId>com.iluwatar</groupId>
31+
<version>1.23.0-SNAPSHOT</version>
32+
</parent>
33+
<modelVersion>4.0.0</modelVersion>
34+
35+
<artifactId>update-method</artifactId>
36+
<dependencies>
37+
<dependency>
38+
<groupId>junit</groupId>
39+
<artifactId>junit</artifactId>
40+
</dependency>
41+
</dependencies>
42+
43+
44+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.updatemethod;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* This pattern simulate a collection of independent objects by telling each to
31+
* process one frame of behavior at a time. The game world maintains a collection
32+
* of objects. Each object implements an update method that simulates one frame of
33+
* the object’s behavior. Each frame, the game updates every object in the collection.
34+
*/
35+
public class App {
36+
37+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
38+
39+
private static final int GAME_RUNNING_TIME = 2000;
40+
41+
/**
42+
* Program entry point.
43+
* @param args runtime arguments
44+
*/
45+
public static void main(String[] args) {
46+
try {
47+
var world = new World();
48+
var skeleton1 = new Skeleton(1, 10);
49+
var skeleton2 = new Skeleton(2, 70);
50+
var statue = new Statue(3, 20);
51+
world.addEntity(skeleton1);
52+
world.addEntity(skeleton2);
53+
world.addEntity(statue);
54+
world.run();
55+
Thread.sleep(GAME_RUNNING_TIME);
56+
world.stop();
57+
} catch (InterruptedException e) {
58+
LOGGER.error(e.getMessage());
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.updatemethod;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* Abstract class for all the entity types.
31+
*/
32+
public abstract class Entity {
33+
34+
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
35+
36+
protected int id;
37+
38+
protected int position;
39+
40+
public Entity(int id) {
41+
this.id = id;
42+
this.position = 0;
43+
}
44+
45+
public abstract void update();
46+
47+
public int getPosition() {
48+
return position;
49+
}
50+
51+
public void setPosition(int position) {
52+
this.position = position;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.updatemethod;
25+
26+
/**
27+
* Skeletons are always patrolling on the game map. Initially all the skeletons
28+
* patrolling to the right, and after them reach the bounding, it will start
29+
* patrolling to the left. For each frame, one skeleton will move 1 position
30+
* step.
31+
*/
32+
public class Skeleton extends Entity {
33+
34+
private static final int PATROLLING_LEFT_BOUNDING = 0;
35+
36+
private static final int PATROLLING_RIGHT_BOUNDING = 100;
37+
38+
protected boolean patrollingLeft;
39+
40+
/**
41+
* Constructor of Skeleton.
42+
*
43+
* @param id id of skeleton
44+
*/
45+
public Skeleton(int id) {
46+
super(id);
47+
patrollingLeft = false;
48+
}
49+
50+
/**
51+
* Constructor of Skeleton.
52+
*
53+
* @param id id of skeleton
54+
* @param postition position of skeleton
55+
*/
56+
public Skeleton(int id, int postition) {
57+
super(id);
58+
this.position = position;
59+
patrollingLeft = false;
60+
}
61+
62+
@Override
63+
public void update() {
64+
if (patrollingLeft) {
65+
position -= 1;
66+
if (position == PATROLLING_LEFT_BOUNDING) {
67+
patrollingLeft = false;
68+
}
69+
} else {
70+
position += 1;
71+
if (position == PATROLLING_RIGHT_BOUNDING) {
72+
patrollingLeft = true;
73+
}
74+
}
75+
logger.info("Skeleton " + id + " is on position " + position + ".");
76+
}
77+
}
78+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.updatemethod;
25+
26+
/**
27+
* Statues shoot lightning at regular intervals.
28+
*/
29+
public class Statue extends Entity {
30+
31+
protected int frames;
32+
33+
protected int delay;
34+
35+
/**
36+
* Constructor of Statue.
37+
*
38+
* @param id id of statue
39+
*/
40+
public Statue(int id) {
41+
super(id);
42+
this.frames = 0;
43+
this.delay = 0;
44+
}
45+
46+
/**
47+
* Constructor of Statue.
48+
*
49+
* @param id id of statue
50+
* @param delay the number of frames between two lightning
51+
*/
52+
public Statue(int id, int delay) {
53+
super(id);
54+
this.frames = 0;
55+
this.delay = delay;
56+
}
57+
58+
@Override
59+
public void update() {
60+
if (++ frames == delay) {
61+
shootLightning();
62+
frames = 0;
63+
}
64+
}
65+
66+
private void shootLightning() {
67+
logger.info("Statue " + id + " shoots lightning!");
68+
}
69+
}

0 commit comments

Comments
 (0)