Abstract Factory Pattern
Explore the Abstract Factory pattern to understand how it enables creation of related object families without depending on their concrete classes. This lesson covers interfaces, concrete factories, and client interaction through a Java example involving aircraft parts, illustrating extensible and maintainable software design.
What is it ?
In the previous lesson, we learned the factory method pattern. We saw how we were able to model the variants of the F-16 using the factory method. But there are numerous airplanes other than F-16 that we'll need to represent. Say the client buys a Boeing-747 for the CEO to travel and now wants your software to provide support for this new type of aircraft.
The abstract factory pattern solves the problem of creating families of related products. For instance, F-16 needs an engine, a cockpit, and wings. The Boeing-747 would require the same set of parts but they would be specific to Boeing. Any airplane would require these three related parts but the parts will be plane and vendor specific. Can you see a pattern emerge here? We need a framework for creating the related parts for each airplane, a family of parts for the F-16, a family of parts for the Boeing-747 so on and so forth.
Formally, the abstract factory pattern is defined as defining an interface to create families of related or dependent objects without specifying their concrete classes.
Class Diagram
The class diagram consists of the following entities
- Abstract Factory
- Concrete Factory
- Abstract Product
- Concrete Product
- Client
Example
An abstract factory can be thought of as a super factor or a factory of factories. The pattern achieves the creation of a family of products without revealing concrete classes to the client. Let's consider an example. Say, you are creating a simulation software for the aviation industry and need to represent different aircraft as objects. But before you represent an aircraft, you also need to represent the different pieces of an aircraft as objects. For now let's pick three: the cockpit, the wings, and the engine. Now say the first aircraft you want to represent is the mighty F-16. You'll probably write three classes one for each piece specific to the F-16. In your code you'll likely consume the just created three classes as follows: