From Wiki.
The abstract factory pattern is similar to the factory pattern, but has the added intent of providing an interface to a factory purposed for the creation of families of related objects in order to enable variations of the factory to be used interchangeably. The abstract factory pattern is a compound pattern incorporating the use of the factory pattern and the interface pattern.
In My words.
Factory is like a service provider for an object. It knows (and follows) the steps needed to create and initialize an object. Client interacts with this Factory to get a complete instance of an object. This is needed when there are series of steps needed to create and initiate an object, instead of relying on user to call the constructor and then call the initialize methods (in proper order) a factory is created which follows all the steps.
On the other hand, initializing inside the constructor is ignored here to so as not to make the constructor complex (code smell).
Abstract factory, is when the factory returns a handle (interface/abstract base class) and takes care of instantiating the correct object as per the parameters passed to the Create method of Factory. Simple example is FileParserFacotry which returns IFileParser, and takes the type of file as parameter. Using the type it instantiates the correct parser (ExcelFileParser, XmlFileParser etc) and returns the handle (IFileParser) to user.
No comments:
Post a Comment