The Abstract Factory pattern is used to delegate the responsibility of creating object to another class. Therefore the factory is implemented as an object and provides an interface for creating families of related classes.
The creation of the class instances itself may be implemented by Factory Methods provided by these classes. The Abstract Factory hides the construction details. Furthermore you may create different Abstract factories implementing the same interface but create different class instances.
The following source code shows an according example. In our software system we have query and command classes to get or set data. Our data handling may be implemented by using a database or files. We offer these query and command classes independent of the type of data store. But of course they must match together. So we want to create different families of related objects. That’s the perfect use case for an Abstract Factory.
At first we define the interface for the factory. Our factory shall provide method to create the query and command classes.
interface IEnvironmentFactory { IQuery GetQuery(); ICommand GetCommand(); }
At next we can implement two different versions of the factory, one for each type of data store. The source code shows the factory relevant code only and does not include the code for the query and command interfaces and classes.
class DatabaseBasedEnvironment : IEnvironmentFactory { public ICommand GetCommand() { return new DatabaseCommand(); } public IQuery GetQuery() { return new DatabaseQuery(); } } class FileBasedEnvironment : IEnvironmentFactory { public ICommand GetCommand() { return new FileCommand(); } public IQuery GetQuery() { return new FileQuery(); } }
This pattern is open for future extensions. For example if you want to add a new data store type, for example a cloud based data store, you can simply add a new factory and add it to you project. In this case there is no need to change the already existing implementations for the database and file based data handling.