Assignment Chef icon Assignment Chef
All English tutorials

Programming lesson

Designing a Scalable RPG System with Command, Factory, and Memento Patterns

Learn how to apply Command, Abstract Factory, and Memento design patterns to build an extensible RPG hero system, inspired by a real assignment from ITP4507 Contemporary Topics in Software Engineering.

Command pattern Abstract Factory pattern Memento pattern RPG system design Open-Closed Principle Java design patterns undo redo pattern game development patterns ITP4507 assignment scalable software engineering contemporary topics software engineering hero system design extensible game architecture factory pattern example memento undo redo

Introduction: Building an Extensible RPG Hero System

In the ever-evolving world of game development, scalability and maintainability are crucial. Imagine you're working on a popular PC RPG called 'Fantastic World' (FW), where players control heroes like Warriors and Warlocks. The game is a hit, but the design must easily accommodate new hero types—Healers, Rangers, etc.—without breaking existing code. This is where the Open-Closed Principle and three key design patterns come into play: Command, Abstract Factory, and Memento. In this tutorial, we'll walk through a system design inspired by a real assignment from ITP4507 Contemporary Topics in Software Engineering, showing you how to implement these patterns in Java. By the end, you'll have a solid foundation for creating flexible, undo/redo-capable game systems.

Understanding the Requirements

The FW system needs to support operations like creating a player, adding/removing heroes, calling hero skills, changing player names, and—importantly—undo and redo for specific actions. The assignment specifies that the design must conform to the Open-Closed Principle and apply three patterns: Command (for operations), Abstract Factory (for creating objects like commands, players, and heroes), and Memento (for undo/redo on 'call hero skill' and 'change player name').

Think of it like a modern multiplayer game where players can customize their roster and abilities. For example, in a battle royale game like Fortnite, players can switch loadouts and undo a purchase if they change their mind. Our design will mirror that flexibility.

Applying the Command Pattern

The Command pattern encapsulates a request as an object, allowing parameterization, queuing, and logging of operations. In FW, each user action (create player, add hero, etc.) becomes a concrete command class that implements a common interface. This decouples the invoker (the menu system) from the actual logic.

public interface Command {
    void execute();
    void undo();
}

For example, CreatePlayerCommand stores the player ID and name, and when executed, it adds the player to a data store. The undo method would remove that player. Similarly, AddHeroCommand adds a hero to the current player, and undo removes it. This pattern also enables the undo/redo list feature, as every executed command can be stored in a history stack.

In the context of current trends, think of a streaming service like Netflix. Each user action (play, pause, add to list) is a command. The service can 'undo' a recently watched episode or 'redo' a skip. The Command pattern provides that structure.

Implementing Abstract Factory for Object Creation

The assignment requires using an Abstract Factory pattern to create different kinds of Command objects and Player/Hero objects. This goes beyond a simple factory; it provides an interface for creating families of related objects without specifying their concrete classes.

public interface GameFactory {
    Player createPlayer(String id, String name);
    Hero createHero(String id, String name, int type);
    Command createCommand(String type, ...);
}

Concrete factories, like WarriorFactory or WarlockFactory, instantiate specific heroes. For commands, a CommandFactory can produce any command based on user input. This ensures that when new hero types are added, you only need to create a new factory and concrete classes without modifying existing code—adhering to the Open-Closed Principle.

Consider the game Genshin Impact, which frequently adds new characters with unique abilities. Using Abstract Factory, the game's system can create new character objects seamlessly without altering the core game loop.

Memento Pattern for Undo/Redo

The Memento pattern is used to capture and restore an object's internal state without violating encapsulation. In FW, it's applied to 'call hero skill' and 'change player name' operations. For example, when a hero's skill is called, the hero's state (HP, MP, etc.) changes. The Memento saves a snapshot of that state before the change, allowing undo to revert it.

public class HeroMemento {
    private int hp;
    private int mp;
    // constructor, getters
}

public class Hero {
    public HeroMemento saveState() { ... }
    public void restoreState(HeroMemento m) { ... }
}

The caretaker (usually the command) stores the memento before executing and uses it during undo. This is similar to version control in collaborative coding platforms like GitHub, where you can revert a file to a previous commit. In gaming, think of a quick-save feature in The Legend of Zelda: Breath of the Wild—you can undo a bad decision by loading a saved state.

Putting It All Together: System Architecture

Now let's see how these patterns integrate. The main loop reads user input, uses the Abstract Factory to create the appropriate Command object, executes it (which may use Memento internally for stateful operations), and pushes the command onto an undo stack. The redo stack stores undone commands. The show undo/redo list command iterates through these stacks.

Here's a simplified class diagram:

  • Command (interface): execute(), undo()
  • ConcreteCommand: CreatePlayerCommand, AddHeroCommand, CallSkillCommand, etc.
  • GameFactory (interface): createPlayer(), createHero(), createCommand()
  • ConcreteFactory: WarriorFactory, WarlockFactory, CommandFactory
  • Memento: HeroMemento, PlayerMemento
  • Caretaker: Command classes that use Memento
  • Originator: Hero, Player classes

This design ensures that adding a new hero type (e.g., Healer) only requires implementing a new concrete hero class and a new factory. No existing code is modified—just extended.

Step-by-Step Implementation Guide

Step 1: Define the Data Model

Create base classes for Player and Hero. Hero is abstract with attributes like id, name, hp. Subclasses: Warrior (with defence), Warlock (with magic damage). Include methods for skill execution and state saving.

Step 2: Implement the Abstract Factory

Define GameFactory interface. Create WarriorFactory and WarlockFactory that implement createHero(). Also create a CommandFactory that takes command type and parameters, returning the appropriate Command object.

Step 3: Implement Commands with Memento

For each operation, create a concrete command class. For 'call hero skill', the command saves the hero's state before executing the skill (which modifies HP/MP). The undo method restores that state. For 'change player name', save the old name.

Step 4: Build the Command Invoker (Main Loop)

The invoker maintains two stacks: undoStack and redoStack. After executing a command, push it onto undoStack and clear redoStack. Undo pops from undoStack, calls undo(), and pushes to redoStack. Redo does the opposite.

Step 5: Test with Sample Data

Create players, add heroes, call skills, change names, and test undo/redo. Ensure that the system correctly restores state and that the undo/redo list displays properly.

Trend Connections: Why This Matters Now

In May 2026, the gaming industry is buzzing with AI-driven NPCs and dynamic world changes. Patterns like Command and Memento are essential for implementing 'undo' in game editors (like Roblox Studio) or for creating replay systems in esports titles like Valorant. The Open-Closed Principle is critical for live-service games that receive monthly updates—think Destiny 2 or World of Warcraft. By mastering these patterns, you're not just solving an assignment; you're building skills for real-world software engineering.

Conclusion

Designing a scalable RPG system using Command, Abstract Factory, and Memento patterns prepares you for complex software projects. The Command pattern encapsulates actions, Abstract Factory ensures extensibility, and Memento provides safe undo/redo. By following this tutorial, you've learned how to apply these patterns to a concrete example from the ITP4507 assignment. Now, go ahead and implement your own Fantastic World system—and remember, the key is to design for change.