dimanche 11 octobre 2015

Using factory method or not?

I've written a simple game (turn based, like Tic Tac Toe). Architecture is very simple, I have Game class which is aggregate root, it has List of players and Board object. Board has Field object - typical aggregate pattern (I think..).

Now, there could be few types of this game: Single Player, Two players (on single device), via Bluetooth, via wifi or online (with a stranger). I have two ways to create Game object. Like this, passing type in constructor:

    public Game(GameType type)
    {
        this.Type = type;
        Players = new List<Player>();

        ...
    }

There will be for sure differences while adding new player/players. Different situation for single player, different for two players, different for online game. So in such method there must be switch/case on type.

OR, second way:

Create derived classes, like SinglePlayerGame : Game etc... Create class like GameAPI with factory method CreateGame.

So, being in that point, I realized I don't know when to use first way (via constructor and then having switch/case) and when use factory method inside additional class..

Thanks in advance for clarifying that!

Aucun commentaire:

Enregistrer un commentaire