Fundamentals 6 min read

Observer Pattern Implementation for Game Events in Java

This article explains how to apply the Observer design pattern in a simple Java game to handle button clicks and hero movement, showing UML diagrams, code examples for subjects, observers, and a client that registers and triggers events.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Observer Pattern Implementation for Game Events in Java

The article demonstrates the use of the Observer pattern to manage user interactions and game events in a small Java game, providing clear scenarios, UML diagrams, and complete code samples.

Scenario 1 – Game UI: The game UI contains two buttons, "Item" and "Magic". Clicking the "Item" button makes the hero use an item, while clicking the "Magic" button makes the hero cast a spell. The goal is to let the hero receive button‑click events in real time.

Scenario 2 – Game Maze: The maze includes a monster, a trap, and a treasure. When the hero moves into the monster's range, the monster attacks; entering the trap's range triggers the trap; entering the treasure's range restores the hero's health. The hero's movement events must be observed by these entities.

UML Overview: The design consists of two groups of objects: observers and subjects. All observers implement the Observer interface, and all subjects inherit from the abstract Subject class, which maintains a list of registered observers.

Subject Class (abstract):

public abstract class Subject {
    private List
observerList = new ArrayList<>();

    public void attachObserver(Observer observer) {
        observerList.add(observer);
    }

    public void detachObserver(Observer observer) {
        observerList.remove(observer);
    }

    public void notifyObservers() {
        for (Observer observer : observerList) {
            observer.update();
        }
    }
}

Observer Interface:

public interface Observer {
    void update();
}

Hero Class (Subject): The hero extends Subject and calls notifyObservers() in its move() method.

public class Hero extends Subject {
    void move() {
        System.out.println("Hero moves forward");
        notifyObservers();
    }
}

Concrete Observers: Monster, Trap, and Treasure each implement Observer and define their own update() logic.

public class Monster implements Observer {
    @Override
    public void update() {
        if (inRange()) {
            System.out.println("Monster attacks the hero!");
        }
    }
    private boolean inRange() { return true; }
}

public class Trap implements Observer {
    @Override
    public void update() {
        if (inRange()) {
            System.out.println("Trap traps the hero!");
        }
    }
    private boolean inRange() { return true; }
}

public class Treasure implements Observer {
    @Override
    public void update() {
        if (inRange()) {
            System.out.println("Treasure heals the hero!");
        }
    }
    private boolean inRange() { return true; }
}

Client Code: The client creates a Hero and the three observers, registers them, and triggers a move.

public class Client {
    public static void main(String[] args) {
        Hero hero = new Hero();
        Monster monster = new Monster();
        Trap trap = new Trap();
        Treasure treasure = new Treasure();
        hero.attachObserver(monster);
        hero.attachObserver(trap);
        hero.attachObserver(treasure);
        hero.move();
    }
}

The program output shows the hero moving forward followed by the monster attack, trap activation, and treasure healing messages, illustrating how the Observer pattern decouples the hero from the specific reactions of game entities.

design patternsJavasoftware architectureGame developmentEvent HandlingObserver Pattern
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.