🏭 Factory Design Pattern
The Factory Method Pattern is a creational design pattern that:
-
Defines an interface for creating objects.
-
Lets subclasses decide which class to instantiate.
-
Decouples the client code from the actual classes being created.
Escape Room Example Scenario
Section titled “Escape Room Example Scenario”In your escape room:
-
You want to create different puzzles (
CipherPuzzle,RiddlePuzzle,LockPuzzle). -
Instead of using
new CipherPuzzle(),new RiddlePuzzle()everywhere, you use a PuzzleFactory to handle creation. -
This makes it easy to add new puzzle types without rewriting client code.
Puzzle Interface
Section titled “Puzzle Interface”// Product interfacepublic interface Puzzle { void play();}Concrete Puzzle Classes
Section titled “Concrete Puzzle Classes”// Cipher puzzlepublic class CipherPuzzle implements Puzzle { @Override public void play() { System.out.println("Decoding a mysterious cipher..."); }}
// Riddle puzzlepublic class RiddlePuzzle implements Puzzle { @Override public void play() { System.out.println("Solving a tricky riddle..."); }}
// Lock puzzlepublic class LockPuzzle implements Puzzle { @Override public void play() { System.out.println("Trying to pick the lock..."); }}Puzzle Factory
Section titled “Puzzle Factory”// Factory classpublic class PuzzleFactory { public static Puzzle createPuzzle(String type) { switch (type.toLowerCase()) { case "cipher": return new CipherPuzzle(); case "riddle": return new RiddlePuzzle(); case "lock": return new LockPuzzle(); default: throw new IllegalArgumentException("Unknown puzzle type: " + type); } }}Using the Factory in the Game
Section titled “Using the Factory in the Game”public class EscapeRoomGame { public static void main(String[] args) { // Create puzzles via factory Puzzle puzzle1 = PuzzleFactory.createPuzzle("cipher"); Puzzle puzzle2 = PuzzleFactory.createPuzzle("riddle"); Puzzle puzzle3 = PuzzleFactory.createPuzzle("lock");
puzzle1.play(); // "Decoding a mysterious cipher..." puzzle2.play(); // "Solving a tricky riddle..." puzzle3.play(); // "Trying to pick the lock..." }}Sample Output
Section titled “Sample Output”Decoding a mysterious cipher...Solving a tricky riddle...Trying to pick the lock...Why is this Useful in a Game?
Section titled “Why is this Useful in a Game?”-
Centralized creation: All puzzle creation is handled in one place.
-
Easy to extend: Add a new
LaserPuzzle→ just add a case inPuzzleFactory. -
Flexibility: You can randomize puzzle generation (pick one at random for each room).
-
Decoupling: The game doesn’t need to know about concrete puzzle classes.
Next Step Ideas 💡
Section titled “Next Step Ideas 💡”-
Create an ItemFactory (flashlight, magnifying glass, lockpick).
-
Use Abstract Factory to generate whole rooms (a set of puzzles + items).
-
Combine with Strategy Pattern so that factory-created puzzles come with a solving strategy.
✅ Key Takeaway:
The Factory Pattern centralizes and abstracts object creation, making it easier to manage puzzles, items, or rooms in your escape room game.