SlideShare a Scribd company logo
Copyright 2016. Jyaasa Technologies. All Right Reserved
ht
tp://jyaasa.com
Factory Design Pattern.
How , Why and When to use
it. Kapil Raj Nakhwa
co-founder : jyaasa.
github.com/xecutioner
@xecutioner303
kapil@jyaasa.com
Intent
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory
method lets class defer instantiation to subclasses
Structure
Motivation
Factory method is primarily used to create an instance of a class in a more
flexible way. Instead of creating instance of a class directly through new, it is
created by another class called as factory.
Implementation
● A factory class can be implemented in several ways, depending
on the type of problem you are trying to solve:
● The Creator class can be an abstract class.
● The Creator class can be a concrete class with the Factory
method as a virtual function.
● The Factory method may be parameterized to allow creation of
different types of objects.
● The Factory method can be a static method.
● The Factory method can be an instance method.
THANK YOU !
QUESTIONS ?
Factory Design Pattern
Factory Design Pattern
Factory Design Pattern
Problem with initialize in pond ?
Factory Design Pattern
Factory Design Pattern
Requirements Changes :
Your users wants plants now in the pond.
Factory Design Pattern
Modify the class Pond to deal with plants as well
Factory Design Pattern
Factory Design Pattern
But I still need those subclasses to continue adding as per the nature of the pond.
Factory Design Pattern
Factory Design Pattern
Oh this is great! , I need more habitats than pond now. Let’s add a jungle
Factory Design Pattern
Okay so the class name pond is not any more appropriate ! let’s rename it to Habitat.
Why is this design still not correct ?
Factory Design Pattern
What if our design now needs more extensions to fungi, insects etc
Introducing abstract Factories
Factory Design Pattern
Factory Design Pattern
Factory Design Pattern
Factory Design Pattern
Factory Design Pattern
Factory Design Pattern
Factory Design Pattern
Advantages
● Let’s discuss
Disadvantages
● let’s discuss
When to use
● When the client doesn't know which class it may require at runtime.
● A class wants its subclasses to specify the objects it creates.
● You want to encapsulate creation of objects.
● Object instance needs to be initialized with some data not available to the client.
● Object instantiation requires lot of data and there are lots of variations based on the data. Instead provide static
Factory methods that create the instances based on different variations.
Related Patterns
● Factory Method has a lot of variations. It can return a new object or the same instance multiple times, or can return
a subclass object by extending a new class.
● Factory Method is usually called through a Template Method and is usually a hook that subclasses can override
for custom implementation. E.g. override the Factory method and provide a different implementation.
● Used to implement Abstract Factory.
● It is a flexible replacement for new as it encapsulates creation.
● Factory Method has to be subclassed if it has to return a new object. Prototypes don’t require a new class; it
requires a new object.
● Prototypes require initialize operations after returning an instance; Factory Methods don’t require such operation.
QUESTIONS ?

More Related Content

PPTX
Design Pattern - Factory Method Pattern
PPTX
Factory Design Pattern
PPTX
Design Patterns - Abstract Factory Pattern
PPTX
Abstract Factory Design Pattern
PPT
Abstract Factory Pattern
PPTX
Factory Method Pattern
PPT
Unit 2-Design Patterns.ppt
PPT
Facade pattern
Design Pattern - Factory Method Pattern
Factory Design Pattern
Design Patterns - Abstract Factory Pattern
Abstract Factory Design Pattern
Abstract Factory Pattern
Factory Method Pattern
Unit 2-Design Patterns.ppt
Facade pattern

What's hot (20)

PPTX
Facade pattern presentation(.pptx)
PDF
Presentation facade design pattern
PPT
Facade pattern
PPT
Design patterns ppt
PPTX
constructors in java ppt
PPTX
Design pattern-presentation
PPT
Introduction to Design Patterns and Singleton
PPTX
Design pattern (Abstract Factory & Singleton)
PDF
Object-oriented Programming-with C#
PPTX
Decision making in JAVA
PPTX
Factory Design Pattern
PPTX
Java constructors
PPTX
Facade Pattern
PPTX
VB.NET:An introduction to Namespaces in .NET framework
PPT
Prototype pattern
PDF
Java Concurrency by Example
PDF
Introduction to Design Pattern
PPT
Builder pattern
PPTX
The Singleton Pattern Presentation
Facade pattern presentation(.pptx)
Presentation facade design pattern
Facade pattern
Design patterns ppt
constructors in java ppt
Design pattern-presentation
Introduction to Design Patterns and Singleton
Design pattern (Abstract Factory & Singleton)
Object-oriented Programming-with C#
Decision making in JAVA
Factory Design Pattern
Java constructors
Facade Pattern
VB.NET:An introduction to Namespaces in .NET framework
Prototype pattern
Java Concurrency by Example
Introduction to Design Pattern
Builder pattern
The Singleton Pattern Presentation
Ad

Similar to Factory Design Pattern (20)

PPTX
Prototype design patterns
PPTX
design patter related ppt and presentation
PPTX
Creational Design Patterns.pptx
PPTX
Javascript Common Design Patterns
ODP
Prototype_pattern
PPT
Layers of Smalltalk Application
PPTX
Factory Method Pattern
PPTX
31 days Refactoring
PPTX
Creational Patterns
PPTX
Effective java
PPS
Jump start to OOP, OOAD, and Design Pattern
PPT
Jump Start To Ooad And Design Patterns
PPTX
PPTX
Object Oriented Programming
PPTX
Code Like a Ninja Session 7 - Creational Design Patterns
ODP
(An Extended) Beginners Guide to Object Orientation in PHP
PPTX
Creating and destroying objects
PPTX
Dependency injection using Google guice
PPTX
Gof design patterns
PDF
Java Design Patterns Interview Questions PDF By ScholarHat
Prototype design patterns
design patter related ppt and presentation
Creational Design Patterns.pptx
Javascript Common Design Patterns
Prototype_pattern
Layers of Smalltalk Application
Factory Method Pattern
31 days Refactoring
Creational Patterns
Effective java
Jump start to OOP, OOAD, and Design Pattern
Jump Start To Ooad And Design Patterns
Object Oriented Programming
Code Like a Ninja Session 7 - Creational Design Patterns
(An Extended) Beginners Guide to Object Orientation in PHP
Creating and destroying objects
Dependency injection using Google guice
Gof design patterns
Java Design Patterns Interview Questions PDF By ScholarHat
Ad

More from Jyaasa Technologies (20)

PPTX
Incident management with jira
PPTX
Extreme programming practices ( xp )
PPTX
The myth of 'real javascript developer'
PPTX
Microservices
PPTX
Facade pattern in rails
PPTX
Scrum ceromonies
PPTX
An introduction to bitcoin
PPTX
Tor network
PPTX
Collective ownership in agile teams
PPTX
Push notification
PPTX
The Design Thinking Process
PPTX
PPTX
Design sprint
PPTX
Data Flow Diagram
PPTX
OKRs and Actions Overview
PPTX
Active record in rails 5
PPTX
Design Patern::Adaptor pattern
PDF
Association in rails
PDF
Web design layout pattern
Incident management with jira
Extreme programming practices ( xp )
The myth of 'real javascript developer'
Microservices
Facade pattern in rails
Scrum ceromonies
An introduction to bitcoin
Tor network
Collective ownership in agile teams
Push notification
The Design Thinking Process
Design sprint
Data Flow Diagram
OKRs and Actions Overview
Active record in rails 5
Design Patern::Adaptor pattern
Association in rails
Web design layout pattern

Recently uploaded (20)

PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PPTX
CHAPTER 2 - PM Management and IT Context
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
Salesforce Agentforce AI Implementation.pdf
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Why Generative AI is the Future of Content, Code & Creativity?
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Reimagine Home Health with the Power of Agentic AI​
17 Powerful Integrations Your Next-Gen MLM Software Needs
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
CHAPTER 2 - PM Management and IT Context
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Operating system designcfffgfgggggggvggggggggg
Oracle Fusion HCM Cloud Demo for Beginners
Salesforce Agentforce AI Implementation.pdf
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
Monitoring Stack: Grafana, Loki & Promtail
Design an Analysis of Algorithms I-SECS-1021-03
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Designing Intelligence for the Shop Floor.pdf
Patient Appointment Booking in Odoo with online payment
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf

Factory Design Pattern

  • 1. Copyright 2016. Jyaasa Technologies. All Right Reserved ht tp://jyaasa.com Factory Design Pattern. How , Why and When to use it. Kapil Raj Nakhwa co-founder : jyaasa. github.com/xecutioner @xecutioner303 kapil@jyaasa.com
  • 2. Intent Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets class defer instantiation to subclasses Structure
  • 3. Motivation Factory method is primarily used to create an instance of a class in a more flexible way. Instead of creating instance of a class directly through new, it is created by another class called as factory.
  • 4. Implementation ● A factory class can be implemented in several ways, depending on the type of problem you are trying to solve: ● The Creator class can be an abstract class. ● The Creator class can be a concrete class with the Factory method as a virtual function. ● The Factory method may be parameterized to allow creation of different types of objects. ● The Factory method can be a static method. ● The Factory method can be an instance method.
  • 12. Requirements Changes : Your users wants plants now in the pond.
  • 14. Modify the class Pond to deal with plants as well
  • 17. But I still need those subclasses to continue adding as per the nature of the pond.
  • 20. Oh this is great! , I need more habitats than pond now. Let’s add a jungle
  • 22. Okay so the class name pond is not any more appropriate ! let’s rename it to Habitat. Why is this design still not correct ?
  • 24. What if our design now needs more extensions to fungi, insects etc
  • 34. When to use ● When the client doesn't know which class it may require at runtime. ● A class wants its subclasses to specify the objects it creates. ● You want to encapsulate creation of objects. ● Object instance needs to be initialized with some data not available to the client. ● Object instantiation requires lot of data and there are lots of variations based on the data. Instead provide static Factory methods that create the instances based on different variations.
  • 35. Related Patterns ● Factory Method has a lot of variations. It can return a new object or the same instance multiple times, or can return a subclass object by extending a new class. ● Factory Method is usually called through a Template Method and is usually a hook that subclasses can override for custom implementation. E.g. override the Factory method and provide a different implementation. ● Used to implement Abstract Factory. ● It is a flexible replacement for new as it encapsulates creation. ● Factory Method has to be subclassed if it has to return a new object. Prototypes don’t require a new class; it requires a new object. ● Prototypes require initialize operations after returning an instance; Factory Methods don’t require such operation.