7/23/2011

Design Pattern - Abstract Factory

Definition
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

UML Class Diagram 


Abstract Factory: when and where use it 
- Constructors are limited in their control over the overall creation process. If your application needs more control, consider using a Factory. Some possible scenarios where this may be the case is when the creation process involves object caching, sharing or re-using of objects, and applications that maintain object and type counts.
- Additionally, there are times when the client does not know exactly what type to construct.  It is easier to code against a base type or an interface and then let a factorymake this decision for the client (based on parameters or other context-based information). Good examples of this are the provider-specific ADO.NET objects (DbConnection, DbCommand, DbDataAdapter, etc).
- Constructors don‟t communicate their intention very well because they must be named after their class (or Sub New in VB).  Having numerous overloaded constructors may make it hard for the client developer to decide which constructor to use. Replacing constructors with intention-revealing creation methods are frequently preferred. Here is an example with 4 overloaded constructors. It is not so clear which one to use.
- We can use abstract class or interface to implement our system with Abstract Factory, but in .NET interface is more prefer to.

Sample

- IProduct

- IFactory

- Concrete product

- Concrete factory

- Client interface

- Client concrete class

Ref: Gang of Four tutorial

No comments:

Post a Comment