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

Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
11prasoon
 

What's hot (20)

Abstract Factory Pattern
Abstract Factory PatternAbstract Factory Pattern
Abstract Factory Pattern
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
Facade pattern
Facade patternFacade pattern
Facade pattern
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
Composite design pattern
Composite design patternComposite design pattern
Composite design pattern
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
 
Prototype design patterns
Prototype design patternsPrototype design patterns
Prototype design patterns
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
 
Facade pattern
Facade patternFacade pattern
Facade pattern
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 
Facade Design Pattern
Facade Design PatternFacade Design Pattern
Facade Design Pattern
 
Builder design pattern
Builder design patternBuilder design pattern
Builder design pattern
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
Presentation facade design pattern
Presentation facade design patternPresentation facade design pattern
Presentation facade design pattern
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design Patterns
 

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

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%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
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
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
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
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
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
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...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 

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.