Why not just call new?
Most of the time, you should. new ChessGame() is clear and costs nothing. The trouble starts when the code that needs an object is written before anyone knows which object it will be.
A small company runs online tournaments for two-player strategy games. Its MatchServer class handles everything that has nothing to do with the rules: it pairs two players, runs a clock for each, relays moves and records the result. Chess came first, so in the middle of startMatch() sits new ChessGame(), and the server calls applyMove() and isOver() on what it gets back. Now the company wants Go. Pairing, clocks and results are exactly the same. The one line that differs is the one that creates the game, and it is buried inside the class everything else depends on.
One creation step, left to subclasses
Factory Method lets a class defer which concrete object to create. MatchServer moves that line into a method of its own, createGame(), calls it from startMatch(), and uses the result only through a Game interface. Subclasses decide what createGame() returns: ChessServer returns a ChessGame, GoServer a GoGame. In the pattern's vocabulary, MatchServer is the Creator, createGame() is the factory method, Game is the Product, and each subclass is a concrete creator making a concrete product. Because the server never names a concrete class, adding Go means writing GoGame and GoServer, and the server's tested code stays as it was.
Each concrete creator overrides createGame(); MatchServer itself only ever names the Game interface.
The same job, done with a switch
There is a simpler way to get new ChessGame() out of startMatch(), and it deserves an honest comparison. A simple factory is one function that takes the game's name and returns the object, makeGame("go"), with a switch inside. It is not one of the GoF patterns, but it is common and often enough: the server calls it, depends only on Game, and never names a concrete class.
What the simple factory lacks is the deferral to subclasses, which is what separates Factory Method from a plain factory function, and it shows in where the next game goes. With the simple factory, adding backgammon means editing the switch, the one function every game passes through. With Factory Method it means adding BackgammonGame and a BackgammonServer subclass, and no existing class changes; extending by adding a class rather than editing a tested one is the open/closed principle from SOLID design principles. One place still learns the new name: whatever picks which server to start, usually the program's entry point or its configuration. The price is a subclass per product. It is worth paying when the Creator is already a class meant to be extended, as a framework's base class usually is, and it is a burden when a subclass would do nothing but override one method.
In that second case there are lighter options: the simple factory, a registry of ready-made instances to clone, which is Prototype, or a factory function or object handed to the server from outside, which is Dependency Injection applied to creation. One requirement survives every option. startMatch() runs once per match and needs a fresh Game each time, with the game's starting position and its own state, so what the server holds must be a way to make games. Handing it a single Game object would share one game across every match.
Where it goes wrong
- Returning the concrete type. A factory method that the Creator declares with a concrete product type instead of the Product interface leaves callers depending on specific classes anyway: declare
MatchServer'screateGame()as returningChessGameand the server's code is free to call chess-only methods, while aGoServeroverride cannot return aGoGameat all. - A parallel hierarchy that grows for nothing. Every new product brings a new creator, and when the creators contain nothing but
createGame(), the hierarchy is ceremony. The Creator may also provide a default implementation of the factory method, still declared to returnGame, so subclasses override it only when they need a different product; and if nothing else calls for a subclass, one of the lighter options above is the honest choice. - Confusing it with its neighbours. Factory Method creates one product. When the creator must produce a matched set, say a game together with its own move validator and renderer that only work with each other, that is Abstract Factory. When the difficulty is assembling one object in many steps, it is Builder.
- Calling
createGame()from the Creator's constructor. The subclass is not fully built at that point (in Java its fields are still unset, in C++ the override does not even run), so call it from an ordinary method such asstartMatch().
Six nodes: a creator, two servers, two games
The Factory Method Pattern challenge allows six nodes, exactly the shape above: a Factory Method node for the Creator, two more for the concrete creators, a Port / Interface node for the Product, and two Component nodes for the concrete products. Then check the arrows. Each concrete creator overrides the factory method and creates its own product, every concrete product implements the interface, and the Creator's only arrow toward the products, the one saying what createGame() returns, goes to the Product interface. If it points at a concrete product, the deferral to subclasses is gone.