The Abstract Factory Pattern is a way of organizing how you create groups of things that are related
to each other. It provides a set of rules or instructions that let you create different types of things
without knowing exactly what those things are. This helps you keep everything organized and lets
you switch between different types easily.
• Abstract Factory pattern is almost same as Factory Pattern and is considered as another layer
of abstraction over factory pattern.
• Abstract Factory patterns work around a super-factory which creates other factories.
• At runtime, the abstract factory is coupled with any desired concrete factory which can
create objects of the desired type.
• The Abstract Factory pattern separates the creation of objects, so clients don’t need to
know specific classes.
• Clients interact with objects through abstract interfaces, keeping class names hidden from
client code.
• Changing the factory allows for different product configurations, as all related products
change together.
• The pattern ensures that an application uses objects from only one family at a time for
better compatibility.
Components of Abstract Factory Pattern
To understand abstract factory pattern, we have to understand the components of it and
relationships between them.
• Abstract Factory:
o Abstract Factory provides a high-level blueprint that defines rules for creating
families of related object without specifying their concrete classes.
o It provides a way such that concrete factories follow a common interface, providing
consistent way to produce related set of objects.
• Concrete Factories:
o Concrete Factories implement the rules specified by the abstract factory. It contain
the logic for creating specific instances of objects within a family.
o Also multiple concrete factories can exist, each produce a distinct family of related
objects.
• Abstract Products:
o Abstract Products represents a family of related objects by defining a set of common
methods or properties.
o It acts as an abstract or interface type that all concrete products within a family must
follow to and provides a unified way for concrete products to be used
interchangeably.
• Concrete Products:
o They are the actual instances of objects created by concrete factories.
o They implement the methods declared in the abstract products, ensuring
consistency within a family and belong to a specific category or family of related
objects.
• Client:
o Client utilizes the abstract factory to create families of objects without specifying
their concrete types and interacts with objects through abstract interfaces provided
by abstract products.
• Below is the code of above problem statement using
Abstract Factory Pattern:
• 1. Abstract Factory Interface (CarFactory)
• "CarFactory" is a Abstract Factory Interface that defines methods
for creating cars and their specifications.
• // Abstract Factory Interface
• interface CarFactory {
• Car createCar();
• CarSpecification createSpecification();
• }
• 2. Concrete Factories (NorthAmericaCarFactory and
EuropeCarFactory)
• "NorthAmericaCarFactory" and "EuropeCarFactory" are concrete
factories that implement the abstract factory interface
"CarFactory" to create cars and specifications specific to North
America, Europe.
• // Concrete Factory for North America Cars
• class NorthAmericaCarFactory implements CarFactory {
• public Car createCar() {
• return new Sedan();
• }
•
• public CarSpecification createSpecification() {
• return new NorthAmericaSpecification();
• }
• }
•
• // Concrete Factory for Europe Cars
• class EuropeCarFactory implements CarFactory {
• public Car createCar() {
• return new Hatchback();
• }
•
• public CarSpecification createSpecification() {
• return new EuropeSpecification();
• }
• }
•
• }
• 3. Abstract Products (Car and CarSpecification interfaces)
• Define interfaces for cars and specifications to ensure a common
structure.
• // Abstract Product Interface for Cars
• interface Car {
• void assemble();
• }
•
• // Abstract Product Interface for Car Specifications
• interface CarSpecification {
• void display();
• }
• 4. Concrete Products (Sedan, Hatchback,
NorthAmericaSpecification, EuropeSpecification)
• "Sedan", "Hatchback", "NorthAmericaSpecification",
"EuropeSpecification" are concrete products that implement the
interfaces to create specific instances of cars and specifications.
• // Concrete Product for Sedan Car
• class Sedan implements Car {
• public void assemble() {
• System.out.println("Assembling Sedan car.");
• }
• }
•
• // Concrete Product for Hatchback Car
• class Hatchback implements Car {
• public void assemble() {
• System.out.println("Assembling Hatchback car.");
• }
• }
•
• // Concrete Product for North America Car Specification
• class NorthAmericaSpecification implements CarSpecification {
• public void display() {
• System.out.println("North America Car Specification: Safety features
compliant with local regulations.");
• }
• }
•
• // Concrete Product for Europe Car Specification
• class EuropeSpecification implements CarSpecification {
• public void display() {
• System.out.println("Europe Car Specification: Fuel efficiency and emissions
compliant with EU standards.");
• }
• }
Complete code for the above example
Below is the complete code for the above example:
•
• // Abstract Factory Interface
• interface CarFactory {
• Car createCar();
• CarSpecification createSpecification();
• }
•
• // Concrete Factory for North America Cars
• class NorthAmericaCarFactory implements CarFactory {
• public Car createCar() {
• return new Sedan();
• }
•
• public CarSpecification createSpecification() {
• return new NorthAmericaSpecification();
• }
• }
•
• // Concrete Factory for Europe Cars
• class EuropeCarFactory implements CarFactory {
• public Car createCar() {
• return new Hatchback();
• }
•
• public CarSpecification createSpecification() {
• return new EuropeSpecification();
• }
• }
•
• // Abstract Product Interface for Cars
• interface Car {
• void assemble();
• }
•
• // Abstract Product Interface for Car Specifications
• interface CarSpecification {
• void display();
• }
•
• // Concrete Product for Sedan Car
• class Sedan implements Car {
• public void assemble() {
• System.out.println("Assembling Sedan car.");
• }
• }
•
• // Concrete Product for Hatchback Car
• class Hatchback implements Car {
• public void assemble() {
• System.out.println("Assembling Hatchback car.");
• }
• }
•
• // Concrete Product for North America Car Specification
• class NorthAmericaSpecification implements CarSpecification {
• public void display() {
• System.out.println("North America Car Specification: Safety features
compliant with local regulations.");
• }
• }
•
• // Concrete Product for Europe Car Specification
• class EuropeSpecification implements CarSpecification {
• public void display() {
• System.out.println("Europe Car Specification: Fuel efficiency and emissions
compliant with EU standards.");
• }
• }
•
•
• // Client Code
• public class CarFactoryClient {
• public static void main(String[] args) {
• // Creating cars for North America
• CarFactory northAmericaFactory = new NorthAmericaCarFactory();
• Car northAmericaCar = northAmericaFactory.createCar();
• CarSpecification northAmericaSpec =
northAmericaFactory.createSpecification();
•
• northAmericaCar.assemble();
• northAmericaSpec.display();
•
• // Creating cars for Europe
• CarFactory europeFactory = new EuropeCarFactory();
• Car europeCar = europeFactory.createCar();
• CarSpecification europeSpec = europeFactory.createSpecification();
•
• europeCar.assemble();
• europeSpec.display();
• }
• }
•
Output
• Assembling Sedan car.
• North America Car Specification: Safety features compliant
with local regulations.
• Assembling Hatchback car.
• Europe Car Specification: Fuel efficiency and emissions
compliant wit...