I would like to check if my understanding in using and implementing factory pattern. Say I am designing an Application to play several Games (chess, poker). The games are loaded from one of the following sources: CSV, XML but I want the ability to add new sources (say DB) without recompiling/redeploying the Application.I just want to deploy the factory logic and the new loader
This suggests a factory pattern for the game loader, something like the following which however causes circular dependencies because the concrete loaders returns a List.
Application.prj has reference to AbstractFactory.prj
-----------------------
main ()
List<Game> games = LoadGameFactory.Create(src).Load(src)
foreach(game in games) game.play();
abstract class Game { .. abstract void Play(); ..}
class Chess : Game { .. void Play() ..}
class Poker : Game { .. void Play() ..}
Factory.prj has reference to Application.prj
------------------------
class LoadGameFactory {
GameLoader Create(string src)
{
if(src == csv) return new LoadCSVGames().Load(src)
else return new LoadXMLGames().Load(src)
}
}
class GameLoader{ List<Game> Load(src) }
class CSVLoader : GameLoader
{
List<Game> Load(src){ ... logic to create Chess() and Poker() from CSV file}
}
class XMLLoader : GameLoader
{
List<Game> Load(src) { ... logic to create Chess() and Poker() from XML file}
}
The solution I found is to create a new project, Application.Model, where I move the Game, Chess, and Poker, ecc That way, Application.prj and Factory.prj will need to reference just Application.Model. And adding DB source requires only factory.prj recompilation and deployment.
Is this the way to go? or is there a way to implement the factory pattern to get the same result. I don't want reflection.