/Battle-Strategy-Patterns

Java/Gradle project demonstrating the Strategy Pattern with interchangeable battle behaviors for monsters, showcasing clean, extensible, and testable object-oriented design.

Primary LanguageJava

Battle Strategy Patterns (Java)

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

Overview

Purpose: A code sample demonstrating strategy patterns for battle or game-like decision-making.

Tech Stack

  • Language: Java
  • Build: Gradle
  • Packaging: Standard src/main/java layout

Project Structure

- 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

Key Concepts

  • 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.

Domain Overview

  • 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.

Behavior Highlights

  • Interchangeable attack strategies via the Strategy Pattern.
  • Clear separation between entity state (Monster) and pluggable behavior (Attack implementations).
  • Deterministic string formats and equality behavior to support logging and testing.

Build

./gradlew clean build

Run

./gradlew run

(Or run the Driver class from your IDE.)