SlideShare a Scribd company logo
1 of 36
Download to read offline
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 ?
Problem with initialize in pond ?
Requirements Changes :
Your users wants plants now in the pond.
Modify the class Pond to deal with plants as well
But I still need those subclasses to continue adding as per the nature of the pond.
Oh this is great! , I need more habitats than pond now. Let’s add a jungle
Okay so the class name pond is not any more appropriate ! let’s rename it to Habitat.
Why is this design still not correct ?
What if our design now needs more extensions to fungi, insects etc
Introducing abstract Factories
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

What's hot

What's hot (20)

Builder design pattern
Builder design patternBuilder design pattern
Builder design pattern
 
Builder Design Pattern (Generic Construction -Different Representation)
Builder Design Pattern (Generic Construction -Different Representation)Builder Design Pattern (Generic Construction -Different Representation)
Builder Design Pattern (Generic Construction -Different Representation)
 
Design Patterns - Factory Method & Abstract Factory
Design Patterns - Factory Method & Abstract FactoryDesign Patterns - Factory Method & Abstract Factory
Design Patterns - Factory Method & Abstract Factory
 
Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
DESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy PatternsDESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy Patterns
 
Prototype Design Pattern
Prototype Design PatternPrototype Design Pattern
Prototype Design Pattern
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
Abstract Factory Pattern (Example & Implementation in Java)
Abstract Factory Pattern (Example & Implementation in Java)Abstract Factory Pattern (Example & Implementation in Java)
Abstract Factory Pattern (Example & Implementation in Java)
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
Abstract Factory Design Pattern
Abstract Factory Design PatternAbstract Factory Design Pattern
Abstract Factory Design Pattern
 
Prototype design patterns
Prototype design patternsPrototype design patterns
Prototype design patterns
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)
 
Abstract Factory Pattern
Abstract Factory PatternAbstract Factory Pattern
Abstract Factory Pattern
 
Bridge pattern
Bridge patternBridge pattern
Bridge pattern
 

Similar to Factory Design Pattern

Layers of Smalltalk Application
Layers of Smalltalk ApplicationLayers of Smalltalk Application
Layers of Smalltalk Application
speludner
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
stanbridge
 

Similar to Factory Design Pattern (20)

Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptx
 
Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design Patterns
 
Prototype_pattern
Prototype_patternPrototype_pattern
Prototype_pattern
 
Layers of Smalltalk Application
Layers of Smalltalk ApplicationLayers of Smalltalk Application
Layers of Smalltalk Application
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
31 days Refactoring
31 days Refactoring31 days Refactoring
31 days Refactoring
 
Creational Patterns
Creational PatternsCreational Patterns
Creational Patterns
 
Effective java
Effective javaEffective java
Effective java
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
 
Sda 8
Sda   8Sda   8
Sda 8
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Code Like a Ninja Session 7 - Creational Design Patterns
Code Like a Ninja Session 7 - Creational Design PatternsCode Like a Ninja Session 7 - Creational Design Patterns
Code Like a Ninja Session 7 - Creational Design Patterns
 
(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP(An Extended) Beginners Guide to Object Orientation in PHP
(An Extended) Beginners Guide to Object Orientation in PHP
 
Creating and destroying objects
Creating and destroying objectsCreating and destroying objects
Creating and destroying objects
 
Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guice
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
Design pattern
Design patternDesign pattern
Design pattern
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 

More from Jyaasa Technologies

More from Jyaasa Technologies (20)

Incident management with jira
Incident management with jiraIncident management with jira
Incident management with jira
 
Extreme programming practices ( xp )
Extreme programming practices ( xp ) Extreme programming practices ( xp )
Extreme programming practices ( xp )
 
The myth of 'real javascript developer'
The myth of 'real javascript developer'The myth of 'real javascript developer'
The myth of 'real javascript developer'
 
Microservices
MicroservicesMicroservices
Microservices
 
Facade pattern in rails
Facade pattern in railsFacade pattern in rails
Facade pattern in rails
 
Scrum ceromonies
Scrum ceromoniesScrum ceromonies
Scrum ceromonies
 
An introduction to bitcoin
An introduction to bitcoinAn introduction to bitcoin
An introduction to bitcoin
 
Tor network
Tor networkTor network
Tor network
 
Collective ownership in agile teams
Collective ownership in agile teamsCollective ownership in agile teams
Collective ownership in agile teams
 
Push notification
Push notificationPush notification
Push notification
 
The Design Thinking Process
The Design Thinking ProcessThe Design Thinking Process
The Design Thinking Process
 
User story
User storyUser story
User story
 
Design sprint
Design sprintDesign sprint
Design sprint
 
Data Flow Diagram
Data Flow DiagramData Flow Diagram
Data Flow Diagram
 
OKRs and Actions Overview
OKRs and Actions OverviewOKRs and Actions Overview
OKRs and Actions Overview
 
Vue.js
Vue.jsVue.js
Vue.js
 
Active record in rails 5
Active record in rails 5Active record in rails 5
Active record in rails 5
 
Design Patern::Adaptor pattern
Design Patern::Adaptor patternDesign Patern::Adaptor pattern
Design Patern::Adaptor pattern
 
Association in rails
Association in railsAssociation in rails
Association in rails
 
Web design layout pattern
Web design layout patternWeb design layout pattern
Web design layout pattern
 

Recently uploaded

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 

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.
  • 6.
  • 7.
  • 8.
  • 10.
  • 11.
  • 12. Requirements Changes : Your users wants plants now in the pond.
  • 13.
  • 14. Modify the class Pond to deal with plants as well
  • 15.
  • 16.
  • 17. But I still need those subclasses to continue adding as per the nature of the pond.
  • 18.
  • 19.
  • 20. Oh this is great! , I need more habitats than pond now. Let’s add a jungle
  • 21.
  • 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 ?
  • 23.
  • 24. What if our design now needs more extensions to fungi, insects etc
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 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.