Lecture 7
Mahbubur Rahman
Lecturer
Department of Software Engineering
Daffodil International University
Factory Pattern (Creational
Design Pattern)
Multiple Object
Creation
● What is Factory Pattern
● Principles of Factory pattern
● Where it is necessary
Outline ● Where it can be implemented
● Real life problem and its
necessary
What is Factory Pattern?
“When you order food on Foodpanda, do you care how the restaurant prepares
it?” No — you just place the order.
The Factory Pattern works exactly like this:
The client (Main Class) requests an object → the factory decides which object to create → client stays
happy and ignorant (in a good way).
ClassName objectName = new constructor();
The Core Problem (Why Factory Pattern Exists?)
Imagine a Payment System for an e-commerce website:
Usage of Factory Design Pattern
Factory Design Pattern provides a common interface for creating objects, while
letting subclasses or a factory class decide which object to Create.
Factory pattern = Centralise object creation
Key Components of Factory Pattern
1. Product (Interface): Defines what the object can do.
2. Concrete Products (Classes): Actual implementations.
3. Factory Class: Contains the creation logic.
UML for Factory Pattern
Interface vs Class
An interface is a contract: it specifies what operations are available, not how they are implemented.
Example: PaymentProcessor interface might promise: pay(amount), refund(transactionId)
A class is a concrete implementation: it includes state + behaviour.
Example: CardPaymentProcessor stores gateway keys, handles card validation rules,
implements pay() differently.
The whole point of the factory is that the caller should not care which concrete
class it got, only that it can call the contract methods.
Factory Pattern Example
Step 1: Create an interface.
Factory Pattern Example
Step 2: Create concrete classes implementing the same interface.
Factory Pattern Example
Step 3: Factory Class for object creation
Factory Pattern Example
Step 4: Create Client/Main Class
References
1. [Link]
2. [Link]
3. [Link]
Thank You