SlideShare a Scribd company logo
1 of 13
Factory Method Pattern
From definition to implementation
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 1
Contents / Agenda
• Definition
• Advantages and Usage
• Implementation – Class diagram
• Implementation – Step 1 to 3
• Implementation – Test and Result
• Difference between Factory and Abstract Factory Patterns
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 2
Definition
• A Factory Pattern or Factory Method Pattern says that just define an
interface or abstract class for creating an object but let the subclasses
decide which class to instantiate.
• In other words, subclasses are responsible to create the instance of
the class.
• The Factory Method Pattern is also known as Virtual Constructor.
• Factory Method pattern comes under “Creational Design Patterns”.
• In Factory pattern, we create object without exposing the creation
logic to the client and refer to newly created object using a common
interface.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 3
Advantages and Usage
• Advantages:
1. Factory Method Pattern allows the sub-classes to choose the type of objects
to create.
2. It promotes the loose-coupling by eliminating the need to bind application-
specific classes into the code. That means the code interacts solely with the
resultant interface or abstract class, so that it will work with any classes that
implement that interface or that extends that abstract class.
• Usage:
1. When a class doesn't know what sub-classes will be required to create
2. When a class wants that its sub-classes specify the objects to be created.
3. When the parent classes choose the creation of objects to its sub-classes.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 4
Implementation - Overview
• We're going to create a Shape interface and concrete classes
implementing the Shape interface. A factory class ShapeFactory is
defined as a next step.
• FactoryPatternDemo, our demo class will use ShapeFactory to get a
Shape object. It will pass information (CIRCLE / RECTANGLE / SQUARE)
to ShapeFactory to get the type of object it needs.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 5
Implementation – Class diagram
• We are going to implement following class diagram with step by step
explanation.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 6
Step 1 : Create Interface
• Our interface is simple, just contains a method to draw a shape.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 7
Step 2 : Concrete classes
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 8
Step 3 : Shape Factory
To keep the
implementation simple,
we can define our factory
class as shown in picture.
This will return
appropriate object.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 9
Step 4 : Test
We can easily test our pattern by
just writing this code in our main
function.
First, create a shape factory and
use its getShape method to draw
new objects.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 10
Step 5 : Results
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 11
Factory Method vs Abstract Factory
• The methods of an Abstract Factory are implemented as Factory Methods.
Both the Abstract Factory Pattern and the Factory Method Pattern
decouples the client system from the actual implementation classes through
the abstract types and factories. The Factory Method creates objects
through inheritance where the Abstract Factory creates objects through
composition.
• With the Factory pattern, you produce implementations (Apple, Banana,
Cherry, etc.) of a particular interface -- say, IFruit. With the Abstract Factory
pattern, you produce implementations of a particular Factory interface --
say, IFruitFactory. Each of those knows how to create different kinds of fruit.
• Factory method: You have a factory that creates objects that derive from a
particular base class.
Abstract factory: You have a factory that creates other factories, and these
factories in turn create objects derived from base classes. You do this
because you often don't just want to create a single object (as with Factory
method) - rather, you want to create a collection of related objects.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 12
Example
• Imagine you are constructing a house and you approach a carpenter
for a door. You give the measurement for the door and your
requirements, and he will construct a door for you. In this case, the
carpenter is a factory of doors. Your specifications are inputs for the
factory, and the door is the output or product from the Factory.
• Now, consider the same example of the door. You can go to a
carpenter, or you can go to a plastic door shop or a PVC shop. All of
them are door factories. Based on the situation, you decide what kind
of factory you need to approach. This is like an Abstract Factory.
17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 13

More Related Content

What's hot

Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
Sara Torkey
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
11prasoon
 

What's hot (20)

Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)
 
Builder design pattern
Builder design patternBuilder design pattern
Builder design pattern
 
Abstract Factory Pattern
Abstract Factory PatternAbstract Factory Pattern
Abstract Factory Pattern
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
 
Design Patterns - Factory Method & Abstract Factory
Design Patterns - Factory Method & Abstract FactoryDesign Patterns - Factory Method & Abstract Factory
Design Patterns - Factory Method & Abstract Factory
 
Introduction to design patterns
Introduction to design patternsIntroduction to design patterns
Introduction to design patterns
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Creational pattern
Creational patternCreational pattern
Creational pattern
 
Flyweight pattern
Flyweight patternFlyweight pattern
Flyweight pattern
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
DESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy PatternsDESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy Patterns
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory 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)
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
Prototype Design Pattern
Prototype Design PatternPrototype Design Pattern
Prototype Design Pattern
 

Similar to Design Pattern - Factory Method Pattern

The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
Carl Lu
 

Similar to Design Pattern - Factory Method Pattern (20)

Javascript Common Design Patterns
Javascript Common Design PatternsJavascript Common Design Patterns
Javascript Common Design Patterns
 
Creational Patterns
Creational PatternsCreational Patterns
Creational Patterns
 
Commonly used design patterns
Commonly used design patternsCommonly used design patterns
Commonly used design patterns
 
Sda 8
Sda   8Sda   8
Sda 8
 
Alex Theedom Java ee revisits design patterns
Alex Theedom	Java ee revisits design patternsAlex Theedom	Java ee revisits design patterns
Alex Theedom Java ee revisits design patterns
 
SE2016 Java Alex Theedom "Java EE revisits design patterns"
SE2016 Java Alex Theedom "Java EE revisits design patterns"SE2016 Java Alex Theedom "Java EE revisits design patterns"
SE2016 Java Alex Theedom "Java EE revisits design patterns"
 
Design pattern - Facade Pattern
Design pattern - Facade PatternDesign pattern - Facade Pattern
Design pattern - Facade Pattern
 
Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptx
 
Module05 arena
Module05 arenaModule05 arena
Module05 arena
 
The Basic Concept Of IOC
The Basic Concept Of IOCThe Basic Concept Of IOC
The Basic Concept Of IOC
 
How to do b tech be projects or any academic projects
How to do b tech be projects or any academic projectsHow to do b tech be projects or any academic projects
How to do b tech be projects or any academic projects
 
JavaScript design patterns introduction
JavaScript design patterns introductionJavaScript design patterns introduction
JavaScript design patterns introduction
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
L07 Frameworks
L07 FrameworksL07 Frameworks
L07 Frameworks
 
Decorator design pattern
Decorator design patternDecorator design pattern
Decorator design pattern
 
Prototype design patterns
Prototype design patternsPrototype design patterns
Prototype design patterns
 
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
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
 
Design Pattern - Introduction
Design Pattern - IntroductionDesign Pattern - Introduction
Design Pattern - Introduction
 

More from Mudasir Qazi

More from Mudasir Qazi (11)

Database - SQL Joins
Database - SQL JoinsDatabase - SQL Joins
Database - SQL Joins
 
Database - Normalization
Database - NormalizationDatabase - Normalization
Database - Normalization
 
Database - Entity Relationship Diagram (ERD)
Database - Entity Relationship Diagram (ERD)Database - Entity Relationship Diagram (ERD)
Database - Entity Relationship Diagram (ERD)
 
OOP - Understanding association, aggregation, composition and dependency
OOP - Understanding association, aggregation, composition and dependencyOOP - Understanding association, aggregation, composition and dependency
OOP - Understanding association, aggregation, composition and dependency
 
OOP - Polymorphism
OOP - PolymorphismOOP - Polymorphism
OOP - Polymorphism
 
OOP - Java is pass-by-value
OOP - Java is pass-by-valueOOP - Java is pass-by-value
OOP - Java is pass-by-value
 
OOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOPOOP - Benefits and advantages of OOP
OOP - Benefits and advantages of OOP
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
 
Design Pattern - Observer Pattern
Design Pattern - Observer PatternDesign Pattern - Observer Pattern
Design Pattern - Observer Pattern
 
Design Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVMDesign Pattern - MVC, MVP and MVVM
Design Pattern - MVC, MVP and MVVM
 
Design Pattern - Chain of Responsibility
Design Pattern - Chain of ResponsibilityDesign Pattern - Chain of Responsibility
Design Pattern - Chain of Responsibility
 

Recently uploaded

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 

Recently uploaded (20)

DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 

Design Pattern - Factory Method Pattern

  • 1. Factory Method Pattern From definition to implementation 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 1
  • 2. Contents / Agenda • Definition • Advantages and Usage • Implementation – Class diagram • Implementation – Step 1 to 3 • Implementation – Test and Result • Difference between Factory and Abstract Factory Patterns 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 2
  • 3. Definition • A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. • In other words, subclasses are responsible to create the instance of the class. • The Factory Method Pattern is also known as Virtual Constructor. • Factory Method pattern comes under “Creational Design Patterns”. • In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 3
  • 4. Advantages and Usage • Advantages: 1. Factory Method Pattern allows the sub-classes to choose the type of objects to create. 2. It promotes the loose-coupling by eliminating the need to bind application- specific classes into the code. That means the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class. • Usage: 1. When a class doesn't know what sub-classes will be required to create 2. When a class wants that its sub-classes specify the objects to be created. 3. When the parent classes choose the creation of objects to its sub-classes. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 4
  • 5. Implementation - Overview • We're going to create a Shape interface and concrete classes implementing the Shape interface. A factory class ShapeFactory is defined as a next step. • FactoryPatternDemo, our demo class will use ShapeFactory to get a Shape object. It will pass information (CIRCLE / RECTANGLE / SQUARE) to ShapeFactory to get the type of object it needs. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 5
  • 6. Implementation – Class diagram • We are going to implement following class diagram with step by step explanation. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 6
  • 7. Step 1 : Create Interface • Our interface is simple, just contains a method to draw a shape. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 7
  • 8. Step 2 : Concrete classes 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 8
  • 9. Step 3 : Shape Factory To keep the implementation simple, we can define our factory class as shown in picture. This will return appropriate object. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 9
  • 10. Step 4 : Test We can easily test our pattern by just writing this code in our main function. First, create a shape factory and use its getShape method to draw new objects. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 10
  • 11. Step 5 : Results 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 11
  • 12. Factory Method vs Abstract Factory • The methods of an Abstract Factory are implemented as Factory Methods. Both the Abstract Factory Pattern and the Factory Method Pattern decouples the client system from the actual implementation classes through the abstract types and factories. The Factory Method creates objects through inheritance where the Abstract Factory creates objects through composition. • With the Factory pattern, you produce implementations (Apple, Banana, Cherry, etc.) of a particular interface -- say, IFruit. With the Abstract Factory pattern, you produce implementations of a particular Factory interface -- say, IFruitFactory. Each of those knows how to create different kinds of fruit. • Factory method: You have a factory that creates objects that derive from a particular base class. Abstract factory: You have a factory that creates other factories, and these factories in turn create objects derived from base classes. You do this because you often don't just want to create a single object (as with Factory method) - rather, you want to create a collection of related objects. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 12
  • 13. Example • Imagine you are constructing a house and you approach a carpenter for a door. You give the measurement for the door and your requirements, and he will construct a door for you. In this case, the carpenter is a factory of doors. Your specifications are inputs for the factory, and the door is the output or product from the Factory. • Now, consider the same example of the door. You can go to a carpenter, or you can go to a plastic door shop or a PVC shop. All of them are door factories. Based on the situation, you decide what kind of factory you need to approach. This is like an Abstract Factory. 17-Dec-14 Mudasir Qazi - mudasirqazi00@gmail.com 13