0% found this document useful (0 votes)
2 views14 pages

Lecture 7 - Factory Method Pattern

The document discusses the Factory Pattern, a creational design pattern that centralizes object creation by allowing a factory class to determine which object to create, keeping the client unaware of the specifics. It outlines the key components of the pattern, including the product interface, concrete products, and factory class, and provides an analogy using food ordering to illustrate its functionality. The lecture also emphasizes the importance of interfaces versus classes in object-oriented design.

Uploaded by

yecasef304
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views14 pages

Lecture 7 - Factory Method Pattern

The document discusses the Factory Pattern, a creational design pattern that centralizes object creation by allowing a factory class to determine which object to create, keeping the client unaware of the specifics. It outlines the key components of the pattern, including the product interface, concrete products, and factory class, and provides an analogy using food ordering to illustrate its functionality. The lecture also emphasizes the importance of interfaces versus classes in object-oriented design.

Uploaded by

yecasef304
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like