mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 08:58:26 +00:00
docs: improve Bytecode pattern documentation
This commit is contained in:
+28
-16
@@ -14,10 +14,7 @@ Allows encoding behavior as instructions for a virtual machine.
|
||||
|
||||
Real world example
|
||||
|
||||
> A team is working on a new game where wizards battle against each other. The wizard behavior
|
||||
> needs to be carefully adjusted and iterated hundreds of times through playtesting. It's not
|
||||
> optimal to ask the programmer to make changes each time the game designer wants to vary the
|
||||
> behavior, so the wizard behavior is implemented as a data-driven virtual machine.
|
||||
> A team is working on a new game where wizards battle against each other. The wizard behavior needs to be carefully adjusted and iterated hundreds of times through playtesting. It's not optimal to ask the programmer to make changes each time the game designer wants to vary the behavior, so the wizard behavior is implemented as a data-driven virtual machine.
|
||||
|
||||
In plain words
|
||||
|
||||
@@ -26,10 +23,7 @@ In plain words
|
||||
[Gameprogrammingpatterns.com](https://gameprogrammingpatterns.com/bytecode.html) documentation
|
||||
states:
|
||||
|
||||
> An instruction set defines the low-level operations that can be performed. A series of
|
||||
> instructions is encoded as a sequence of bytes. A virtual machine executes these instructions one
|
||||
> at a time, using a stack for intermediate values. By combining instructions, complex high-level
|
||||
> behavior can be defined.
|
||||
> An instruction set defines the low-level operations that can be performed. A series of instructions is encoded as a sequence of bytes. A virtual machine executes these instructions one at a time, using a stack for intermediate values. By combining instructions, complex high-level behavior can be defined.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
@@ -60,9 +54,7 @@ public class Wizard {
|
||||
}
|
||||
```
|
||||
|
||||
Next, we show the available instructions for our virtual machine. Each of the instructions has its
|
||||
own semantics on how it operates with the stack data. For example, the ADD instruction takes the top
|
||||
two items from the stack, adds them together and pushes the result to the stack.
|
||||
Next, we show the available instructions for our virtual machine. Each of the instructions has its own semantics on how it operates with the stack data. For example, the ADD instruction takes the top two items from the stack, adds them together and pushes the result to the stack.
|
||||
|
||||
```java
|
||||
@AllArgsConstructor
|
||||
@@ -84,8 +76,7 @@ public enum Instruction {
|
||||
}
|
||||
```
|
||||
|
||||
At the heart of our example is the `VirtualMachine` class. It takes instructions as input and
|
||||
executes them to provide the game object behavior.
|
||||
At the heart of our example is the `VirtualMachine` class. It takes instructions as input and executes them to provide the game object behavior.
|
||||
|
||||
```java
|
||||
@Getter
|
||||
@@ -225,17 +216,38 @@ Here is the console output.
|
||||
|
||||
## Applicability
|
||||
|
||||
Use the Bytecode pattern when you have a lot of behavior you need to define and your
|
||||
game’s implementation language isn’t a good fit because:
|
||||
Use the Bytecode pattern when you have a lot of behavior you need to define and your game’s implementation language isn’t a good fit because:
|
||||
|
||||
* It’s too low-level, making it tedious or error-prone to program in.
|
||||
* Iterating on it takes too long due to slow compile times or other tooling issues.
|
||||
* 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.
|
||||
|
||||
## Known Uses
|
||||
|
||||
* Java Virtual Machine (JVM) uses bytecode to allow Java programs to run on any device that has JVM installed
|
||||
* Python compiles its scripts to bytecode which is then interpreted by Python Virtual Machine
|
||||
* The .NET Framework uses a form of bytecode called Microsoft Intermediate Language (MSIL)
|
||||
|
||||
## Consequences
|
||||
|
||||
Benefits:
|
||||
|
||||
* Portability: Programs can run on any platform that has a compatible VM.
|
||||
* Security: The VM can enforce security checks on the bytecode.
|
||||
* Performance: JIT compilers can optimize bytecode at runtime, potentially improving performance over interpreted code.
|
||||
|
||||
Trade-offs:
|
||||
|
||||
* Overhead: Running bytecode typically involves more overhead than running native code, potentially affecting performance.
|
||||
* Complexity: Implementing and maintaining a VM adds complexity to the system.
|
||||
|
||||
## Related patterns
|
||||
|
||||
* [Interpreter](https://java-design-patterns.com/patterns/interpreter/)
|
||||
* [Interpreter](https://java-design-patterns.com/patterns/interpreter/) is often used within the implementation of VMs to interpret bytecode instructions
|
||||
* [Command](https://java-design-patterns.com/patterns/command/): Bytecode instructions can be seen as commands executed by the VM.
|
||||
* [Factory Method](https://java-design-patterns.com/patterns/factory-method/): VMs may use factory methods to instantiate operations or instructions defined in the bytecode.
|
||||
|
||||
## Credits
|
||||
|
||||
* [Game programming patterns](http://gameprogrammingpatterns.com/bytecode.html)
|
||||
* [Programming Language Pragmatics](https://amzn.to/49Tusnn)
|
||||
|
||||
Reference in New Issue
Block a user