Design Pattern: Abstract Factory
Abstract Factory
Type: Object Creational
Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. This will give you the ability to have diverse implementations for a common interface.
Rough Description: Abstract Factory is the interface that you define to encapsulate all the objects of a certain family. From there you can create what are called "Concrete Factory", which actually instantiates the family of objects for you. Your client or consumer will only know of the interface. Somewhere in your code, you will set the interface to your Concrete Factory which will do the heavy lifting of instantiating your objects.
Applicability
- A system should be independent of how its products should be created, composed, or represented.
- A system should be configured with one of multiple families of products.
- A family of related products need to be configured together, and relations enforced.
- You want to provide a class of product, but don't want to reveal their implementations.
Participants
- Abstract Factory - Interface for construction operations
- Concrete Factory - Implements the operations for creating concrete objects
- Abstract Product - Interface for declaring the properties of an object
- Concrete Product - Implements the objects that will be created
- Client - Only uses the interfaces defined by Abstract Factory and Abstract Product classes
Collaborations
- Normally a single instance of Concrete Factory is created at run-time.
- Abstract Factory defers product creation to its Concrete Factory Sub-classes.
Consequences
- Isolates concrete classes - Enables you to control what objects your applications creates. Decouples the object's names from your client code. [ + ]
- It makes exchanging product families easier - Because your code does not explicitly say what is being created, you can swap out the Concrete Factory and create your new family. [ + ]
- Promotes consistency between products - Makes sure that as you create families of objects, there is a parity between families. [ + ]
- Supporting new kinds of families becomes more difficult - If you need to add support for some new objects in one family, you will need to implement this in all sub classes of your factory interface. [ - ]
Comments
Post a Comment