Factory Method
The Factory Method design pattern focuses on the creation of objects. However, you might wonder why we need a method for object creation when constructors already serve that purpose. The answer lies in the limitations of constructors.
To illustrate this, let’s consider the example of building a chess game. Suppose we want to allow players to save the game state into a text file and later restore the game from that position.
Since the size of the chessboard is predetermined, we only need to record the position and type of each piece. We can use algebraic notation for this purpose. For instance, the Queen piece at C3 will be stored in the file as qc3
, the pawn piece at A8 as pa8
, and so on.
Now, let’s assume that we have already read this file into a list of strings. This application of the Singleton design pattern, as we discussed earlier, would be an excellent fit.
Given the list of notations, our goal is to populate the chessboard with...