0% found this document useful (0 votes)
2 views

AbstractFactoryDesignPattern

The Abstract Factory Design Pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It promotes loose coupling and is widely used in libraries like JDK and Spring framework. The pattern involves defining interfaces and abstract classes for vehicles, with concrete implementations for specific vehicle types, allowing for flexible and consistent object creation while managing complexity.

Uploaded by

Himanshu Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

AbstractFactoryDesignPattern

The Abstract Factory Design Pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It promotes loose coupling and is widely used in libraries like JDK and Spring framework. The pattern involves defining interfaces and abstract classes for vehicles, with concrete implementations for specific vehicle types, allowing for flexible and consistent object creation while managing complexity.

Uploaded by

Himanshu Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Abstract Factory Design Pattern

one of the most commonly used design


pattern
part of creational design pattern
uses in almost all the libraries in JDK,
Spring framework.

define an interface ( A java interface or an


abstract class) for creating object and let
the subclasses decide which class to
instantiate.

promotes loose-coupling by eliminating


the need to bind application-specific
classes to the code

When to use Abstract Design pattern?


objects does not need details of how classes are
created and represented
system need to operate with one of several families of
products
system needs to be configured with one of a multiple
Abstract Factory Design Pattern
family of objects.
provide a library and want to show just
the interface, not implementation of
the library components.

Class Diagram example


Abstract Factory Design Pattern
Pseudo code for the example:
1) interface Vehicle
book(int distance) define generic vehicle
setVehicleType()
setBaseCost()
setVehicleChargesPerUnitDistance()
int calculateCostOfBooking(int distance)

2) abstract class Car implements Vehicle


String carType
int baseCost
define abstract class Car that
int chargesPerUnitDistance
implement Vehicle

book(int distance){
setVehicleType()
setBaseCost()
setVehicleChargesPerUnitDistance()
int cost = calculateCostOfBooking(distance)
print(carType + ", " + distance + ", " + cost + ". ")

int calculateCostOfBooking(int distance){


int serviceCharge = 3
return baseCost + chargesPerUnitDistance * distance + serviceCharge

3) abstract class Bike implements Vehicle


String bikeType
int baseCost
int chargesPerUnitDistance

define abstract class Bike that


book(int distance)
implement Vehicle
setVehicleType()
setBaseCost()
setVehicleChargesPerUnitDistance()
int cost = calculateCostOfBooking(distance)
print(bikeType+ "," + distance + "," + cost + ". ")
Abstract Factory Design Pattern

int calculateCostOfBooking(int distance){


int serviceCharge = 3
return baseCost + chargesPerUnitDistance * distance + serviceCharge

4) class MicroCar extends Car

MicroCar()
define concrete class MicroCar
setVehicleType() for Car
carType = "Micro"

setBaseCost()
baseCost = 50

setVehicleChargesPerUnitDistance()
chargesPerUnitDistance = 10

5) class SportsBike extends Bike

PersonalAuto()

setVehicleType()
define concrete class SportsBike
bikeType = "Sports"
for Bike

setBaseCost()
baseCost = 10

setVehicleChargesPerUnitDistance()
chargesPerUnitDistance = 15
define abstract class
6) abstract class AbstractVehicleFactory
AbstractVehicleFactory which
abstract Vehicle getVehicle(String type)
is factory of factory
Abstract Factory Design Pattern

7) class CarFactory extends AbstractVehicleFactory

Vehicle getVehicle(String type)


if(type.equalsIgnoreCase("Micro")) define concrete class
return new MicroCar() CarFactory that extends
else if(type.equalsIgnoreCase("Mini")) AbstractVehicleFactory
return new MiniCar()
else if(type.equalsIgnoreCase("Mega"))
return new MegaCar()
else
return new MiniCar()

8) class BikeFactory extends AbstractVehicleFactory

Vehicle getVehicle(String type)


if(type.equalsIgnoreCase("Sports"))
define concrete class
return new SportsBike()
BikeFactory that extends
else if(type.equalsIgnoreCase("Normal"))
AbstractBikeFactory
return new NormalBike()
else
return new NormalBike()

9) class FactoryProvider
static AbstractVehicleFactory getVehicleFactory(String factoryType)
if(factoryType.equalsIgnoreCase("Car"))
return new CarFactory()
else if(factoryType.equalsIgnoreCase("Auto")) define FactoryProvider that
return new AutoFactory() provides required vehicle factory
else if(factoryType.equalsIgnoreCase("Bike"))
return new BikeFactory()
else
return new CarFactory()
Abstract Factory Design Pattern

10) class AbstractFactoryPatternDemoClient


main()
int distance = 10
Client class
/*
* Book a Micro Car for a distance of 10 kms
*/
AbstractVehicleFactory carFactory = FactoryProvider.getVehicleFactory("Car")
Vehicle miniCar = carFactory.getVehicle("Micro")
miniCar.book(distance)

/*
* Book a Personal Auto for a distance of 10 kms
*/
AbstractVehicleFactory autoFactory = FactoryProvider.getVehicleFactory("Auto")
Vehicle personalAuto = autoFactory.getVehicle("Personal")
personalAuto.book(distance)
Abstract Factory Design Pattern
Advantages
offers loose coupling in the code
supports open close principle since the
code can be easily extended for
supporting new classes
enforces consistent creation of objects

Disadvantages
increased complexity

You might also like