In simple terms, an abstract factory is a class that provides an interface to produce a family of objects. In the Java programming language, it can be implemented either as an interface or as an abstract class.
In the context of an abstract factory there exist:
- Suites or families of related, dependent classes.
- A group of concrete factory classes that implements the interface provided by the abstract factory class. Each of these factories controls or provides access to a particular suite of related, dependent objects and implements the abstract factory interface in a manner that is specific to the family of classes it controls.
The Abstract Factory pattern is useful when a client object wants to create an instance of one of a suite of related, dependent classes without having to know which specific concrete class is to be instantiated. In the absence of an abstract factory, the required implementation to select an appropriate class (in other words, the class selection criterion) needs to be present everywhere such an instance is created. An abstract factory helps avoid this duplication by providing the necessary interface for creating such instances.
This is useful for plugging in a different group of objects to alter the behavior of the system.
This situation often arises when designing a framework or a library, which needs to be kept extensible. One example is the JDBC (Java Database Connectivity) driver system.
ProtoType
Example
Select Cars and SUV under the categories of Luxury and NonLuxury.
No comments:
Post a Comment