A concise Java/Gradle implementation showcasing the Strategy Pattern applied to a simple battle simulation. The code organizes interchangeable behaviors as strategies to keep domain objects clean, extensible, and testable. For sample I/O and additional details, see: https://www.rsiddiq.com/software-design.html
Purpose: A code sample demonstrating strategy patterns for battle or game-like decision-making.
- Language: Java
- Build: Gradle
- Packaging: Standard
src/main/javalayout
- Battle-Strategy-Patterns/
- .gitattributes
- .gitignore
- app/
- build.gradle
- src/
- main/
- java/
- Abilities/
- Ability.java
- Attack.java
- MeleeAttack.java
- RangedAttack.java
- Driver.java
- Monsters/
- Imp.java
- Kobold.java
- Monster.java
- test/
- java/
- DemoTest.java
- gradle/
- libs.versions.toml
- wrapper/
- gradle-wrapper.jar
- gradle-wrapper.properties
- gradle.properties
- gradlew
- gradlew.bat
- settings.gradle
- Strategy Pattern: Encapsulate algorithms (tactics) behind a common interface and pass them into a context (e.g., a Unit, Player, or Battle Engine) at runtime.
- Open/Closed Principle: Add new strategies without modifying existing code.
- Testability: Strategies are isolated, making them easy to unit test.
- Monsters package
- Monster (abstract): base entity with core state and behavior (e.g., hp/maxHP/xp), string formatting, and equality/hash semantics.
- Imp, Kobold: concrete monsters extending
Monster.
- Abilities package
- Ability: tagging interface.
- Attack: strategy interface defining
attack(Monster target). - MeleeAttack, RangedAttack: concrete strategies injected into monsters.
- Interchangeable attack strategies via the Strategy Pattern.
- Clear separation between entity state (
Monster) and pluggable behavior (Attackimplementations). - Deterministic string formats and equality behavior to support logging and testing.
./gradlew clean build./gradlew run(Or run the Driver class from your IDE.)