In the following two posts:
we have seen two snippets of code that are considered Factory Methods.
We talked about the code below in the Enterprise Library Data Access Application Block:
The CreateDatabase() method is considered a Factory Method in that the client is isolated from knowing which concrete class to instantiate during runtime. The concrete class that will be instantiated is located in the dataConfiguration.config file. I've cut down the config file quite a bit, but you can see the concrete type to be instantiated below as Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase in the assembly Microsoft.Practices.EnterpriseLibrary.Data.
We saw the Factory Method in the Community Server source code as well:
The Instance() method is the Factory Method in this case. Again, the client code is unaware of the concrete class to be instantiated as the WeblogDataProvider, because it is tucked away in the communityserver.config file:
The fact that a method creates a new object does not in itself mean that it is an example of the Factory Method pattern. The Factory Method pattern requires that an operation that creates an object also isolates the client from knowing which class to instantiate. When a client requests a new object, the precise class of the object that is created depends on the behavior of the object receiving the creation request.