Welcome to our new series of articles, PHP Design Patterns Games! In this article, we will explore another creational design pattern, the Factory Pattern. The Factory Pattern is used to create objects without having to specify the exact class of object that will be created. Instead, the Factory Pattern provides a generic interface to create objects, and allows subclasses to decide which class to instantiate. We will demonstrate how to use the Factory Pattern to create an abstract Game class that could be used for example in a TicTacToe or Chess game …etc.

Factory Pattern

Let’s assume we have an abstract Game class, and two concrete classes, TicTacToe and Chess, that extend it. We also have a GameFactory class that can create instances of these concrete classes.

phpCopy code// Abstract Game class
abstract class Game {
  abstract function getName();
  abstract function getRules();
}

// Concrete TicTacToe class
class TicTacToe extends Game {
  function getName() {
    return "Tic Tac Toe";
  }

  function getRules() {
    return "Players take turns placing X's and O's on a 3x3 grid. The first player to get three in a row wins!";
  }
}

// Concrete Chess class
class Chess extends Game {
  function getName() {
    return "Chess";
  }

  function getRules() {
    return "Two players use pieces to attack and capture the opponent's pieces, with the goal of checkmating the opponent's king.";
  }
}

// GameFactory class
class GameFactory {
  function createGame($gameType) {
    switch($gameType) {
      case "TicTacToe":
        return new TicTacToe();
        break;
      case "Chess":
        return new Chess();
        break;
      default:
        throw new Exception("Invalid game type");
        break;
    }
  }
}

In this example, the Game class is an abstract class with two abstract methods: getName() and getRules(). TicTacToe and Chess are concrete classes that extend the Game class and implement their own versions of these methods.

The GameFactory class is a factory class that can create instances of TicTacToe and Chess based on the gameType parameter passed to the createGame() method. If an invalid game type is passed, it throws an exception.

Here’s an example of how you could use the GameFactory class to create instances of the TicTacToe and Chess classes:

phpCopy code// Create a GameFactory instance
$factory = new GameFactory();

// Create a TicTacToe game instance
$ticTacToe = $factory->createGame("TicTacToe");
echo $ticTacToe->getName() . "\n"; // Output: Tic Tac Toe
echo $ticTacToe->getRules() . "\n"; // Output: Players take turns placing X's and O's on a 3x3 grid. The first player to get three in a row wins!

// Create a Chess game instance
$chess = $factory->createGame("Chess");
echo $chess->getName() . "\n"; // Output: Chess
echo $chess->getRules() . "\n"; // Output: Two players use pieces to attack and capture the opponent's pieces, with the goal of checkmating the opponent's king.

In this example, we create a GameFactory instance and use it to create instances of the TicTacToe and Chess classes. We then call the getName() and getRules() methods on each instance to get their respective names and rules.

In conclusion, the Factory Pattern is a powerful tool for creating objects in a flexible and extensible way. By using the Factory Pattern, we can decouple object creation from the rest of the code, which makes it easier to maintain and test. We have seen how we can use the Factory Pattern to create an abstract Game class that can be extended by subclasses to create specific types of games. We hope that this article has given you a good introduction to the Factory Pattern, and that you can use this knowledge to create more flexible and maintainable code in your own projects.

LEAVE A REPLY

Please enter your comment!
Please enter your name here