Skip to content

Commit 318f811

Browse files
pzawitowskiiluwatar
authored andcommitted
Bytecode pattern iluwatar#553 (iluwatar#896)
* Added bytecode pattern * Diagram changed and added licence information * Added bytecode module to main pom. * Fixed missing dependency error
1 parent 7f6067f commit 318f811

13 files changed

+819
-0
lines changed

bytecode/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
layout: pattern
3+
title: Bytecode
4+
folder: bytecode
5+
permalink: /patterns/bytecode/
6+
categories: Behavioral
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
---
11+
12+
## Intent
13+
Allows to encode behaviour as instructions for virtual machine.
14+
15+
## Applicability
16+
Use the Bytecode pattern when you have a lot of behavior you need to define and your
17+
game’s implementation language isn’t a good fit because:
18+
19+
* it’s too low-level, making it tedious or error-prone to program in.
20+
* iterating on it takes too long due to slow compile times or other tooling issues.
21+
* it has too much trust. If you want to ensure the behavior being defined can’t break the game, you need to sandbox it from the rest of the codebase.
22+
23+
## Credits
24+
25+
* [Game programming patterns](http://gameprogrammingpatterns.com/bytecode.html)

bytecode/etc/bytecode.png

19.4 KB
Loading

bytecode/etc/bytecode.ucls

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.2.3" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
3+
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="com.iluwatar.bytecode.VirtualMachine" project="bytecode"
5+
file="/bytecode/src/main/java/com/iluwatar/bytecode/VirtualMachine.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="455" y="173"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<class id="2" language="java" name="com.iluwatar.bytecode.App" project="bytecode"
14+
file="/bytecode/src/main/java/com/iluwatar/bytecode/App.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="148" y="110"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</class>
22+
<class id="3" language="java" name="com.iluwatar.bytecode.Wizard" project="bytecode"
23+
file="/bytecode/src/main/java/com/iluwatar/bytecode/Wizard.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="148" y="416"/>
25+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</class>
31+
<association id="4">
32+
<end type="SOURCE" refId="1" navigable="false" variant="ASSOCIATION">
33+
<attribute id="5" name="wizards">
34+
<position height="18" width="48" x="296" y="291"/>
35+
</attribute>
36+
<multiplicity id="6" minimum="0" maximum="2147483647">
37+
<position height="0" width="0" x="-327" y="-27"/>
38+
</multiplicity>
39+
</end>
40+
<end type="TARGET" refId="3" navigable="true" variant="ASSOCIATION"/>
41+
<display labels="true" multiplicity="true"/>
42+
</association>
43+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
44+
sort-features="false" accessors="true" visibility="true">
45+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
46+
<operations public="true" package="true" protected="true" private="true" static="true"/>
47+
</classifier-display>
48+
<association-display labels="true" multiplicity="true"/>
49+
</class-diagram>

bytecode/pom.xml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2019 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<parent>
30+
<artifactId>java-design-patterns</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.21.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>bytecode</artifactId>
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.junit.jupiter</groupId>
40+
<artifactId>junit-jupiter-engine</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 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+
package com.iluwatar.bytecode;
24+
25+
import com.iluwatar.bytecode.util.InstructionConverterUtil;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* The intention of Bytecode pattern is to give behavior the flexibility of data by encoding it as instructions
31+
* for a virtual machine.
32+
* An instruction set defines the low-level operations that can be performed. A series of instructions is encoded as
33+
* a sequence of bytes. A virtual machine executes these instructions one at a time,
34+
* using a stack for intermediate values. By combining instructions, complex high-level behavior can be defined.
35+
*
36+
* This pattern should be used when there is a need to define high number of behaviours and implementation engine
37+
* is not a good choice because
38+
* It is too lowe level
39+
* Iterating on it takes too long due to slow compile times or other tooling issues.
40+
* It has too much trust. If you want to ensure the behavior being defined can’t break the game,
41+
* you need to sandbox it from the rest of the codebase.
42+
*
43+
*/
44+
public class App {
45+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
46+
47+
/**
48+
* Main app method
49+
* @param args command line args
50+
*/
51+
public static void main(String[] args) {
52+
VirtualMachine vm = new VirtualMachine();
53+
54+
Wizard wizard = new Wizard();
55+
wizard.setHealth(45);
56+
wizard.setAgility(7);
57+
wizard.setWisdom(11);
58+
vm.getWizards()[0] = wizard;
59+
60+
interpretInstruction("LITERAL 0", vm);
61+
interpretInstruction( "LITERAL 0", vm);
62+
interpretInstruction( "GET_HEALTH", vm);
63+
interpretInstruction( "LITERAL 0", vm);
64+
interpretInstruction( "GET_AGILITY", vm);
65+
interpretInstruction( "LITERAL 0", vm);
66+
interpretInstruction( "GET_WISDOM ", vm);
67+
interpretInstruction( "ADD", vm);
68+
interpretInstruction( "LITERAL 2", vm);
69+
interpretInstruction( "DIVIDE", vm);
70+
interpretInstruction( "ADD", vm);
71+
interpretInstruction( "SET_HEALTH", vm);
72+
}
73+
74+
private static void interpretInstruction(String instruction, VirtualMachine vm) {
75+
InstructionConverterUtil converter = new InstructionConverterUtil();
76+
vm.execute(converter.convertToByteCode(instruction));
77+
LOGGER.info(instruction + String.format("%" + (12 - instruction.length()) + "s", "" ) + vm.getStack());
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 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+
package com.iluwatar.bytecode;
24+
25+
/**
26+
* Representation of instructions understandable by virtual machine
27+
*/
28+
public enum Instruction {
29+
30+
LITERAL(1),
31+
SET_HEALTH(2),
32+
SET_WISDOM (3),
33+
SET_AGILITY(4),
34+
PLAY_SOUND(5),
35+
SPAWN_PARTICLES(6),
36+
GET_HEALTH(7),
37+
GET_AGILITY(8),
38+
GET_WISDOM(9),
39+
ADD(10),
40+
DIVIDE (11);
41+
42+
private int value;
43+
44+
Instruction(int value) {
45+
this.value = value;
46+
}
47+
48+
public int getIntValue() {
49+
return value;
50+
}
51+
52+
/**
53+
* Converts integer value to Instruction
54+
* @param value value of instruction
55+
* @return representation of the instruction
56+
*/
57+
public static Instruction getInstruction(int value) {
58+
for (int i = 0; i < Instruction.values().length; i++) {
59+
if (Instruction.values()[i].getIntValue() == value) {
60+
return Instruction.values()[i];
61+
}
62+
}
63+
throw new IllegalArgumentException("Invalid instruction value");
64+
}
65+
}

0 commit comments

Comments
 (0)