SlideShare a Scribd company logo
Design Patterns
CSE354
Dr. Muhammad Farhan
Assistant Professor, CUI Sahiwal
farhan@cuisahiwal.edu.pk
+92-3324428802
Factory Method
DESIGN PATTERN
Factory
Method
Factory Method is a
creational design pattern
that provides
an interface for creating
objects in a superclass,
but allows
subclasses to alter the
type of objects that will be
created.
Problem (Cont…)
Imagine that you’re creating a logistics management
application. The first version of your app can only handle
transportation by trucks, so the bulk of your code lives inside
the
Truck class.
After a while, your app becomes pretty popular. Each day you
receive dozens of requests from sea transportation companies
to incorporate sea logistics into the app.
Adding a new class to the program isn’t that simple if the rest of the code
is already coupled to existing classes
Problem
Great news, right? But how about the code? At present, most of
your code is coupled to the Truck class. Adding Ships into the app
would require making changes to the entire codebase. Moreover,
if later you decide to add another type of transportation to the
app, you will probably need to make all of these changes again.
As a result, you will end up with pretty nasty code, riddled with
conditionals that switch the app’s behavior depending on the
class of transportation objects.
Solution (Cont…)
The Factory Method pattern suggests that you
replace direct object construction calls (using
the new operator) with calls to a special factory
method. Don’t worry: the objects are still
created via the new operator, but it’s being
called from within the factory method. Objects
returned by a factory method are often referred
to as “products.
Solution (Cont…)
At first glance, this change may look pointless: we just moved the
constructor call from one part of the program to another. However,
consider this: now you can override the factory method in a subclass and
change the class of products being created by the method.
There’s a slight limitation though: subclasses may return different types of
products only if these products have a common base class or interface.
Also, the factory method in the base class should have its return type
declared as this interface.
Solution (Cont...)
For example, both Truck and Ship classes
should implement the Transport interface,
which declares a method called deliver . Each
class implements this method differently:
trucks deliver cargo by land; ships deliver
cargo by sea. The factory method in the
RoadLogistics class returns truck objects,
whereas the factory method in the
SeaLogistics class returns ships.
Solution (Cont…)
The code that uses the factory
method (often called the client
code) doesn’t see a difference
between the actual products
returned by various
subclasses. The client treats all
the products as abstract
Transport .
Solution
The client knows that all transport objects are supposed to
have the deliver method, but exactly how it works isn’t
important to the client.
Structure
(Cont…)
Structure (Cont…)
The Product declares the interface, which is common to all objects that can be
produced by the creator and its subclasses.
Concrete Products are different implementations of the product interface.
The Creator class declares the factory method that returns new product
objects. It’s important that the return type of this method matches the product
interface.
You can declare the factory method as abstract to force all subclasses to
implement their own versions of the method. As an alternative, the base
factory method can return some default product type
Structure (Cont…)
Note, despite its name, product creation is not the primary responsibility
of the creator. Usually, the creator class already has some core business
logic related to products. The factory
method helps to decouple this logic from the concrete product classes.
Here is an analogy: a large software development company can have a
training department for programmers. However, the primary function of
the company as a whole is still writing code, not producing programmers.
Structure
Concrete Creators override the base factory method, so it returns a different
type of product. 76 Creational Design Patterns / Factory Method Note that
the factory method doesn’t have to create new instances all the time. It can
also return existing objects from a cache, an object pool, or another source.

More Related Content

PPT
Unit 2-Design Patterns.ppt
PPT
Design Pattern For C# Part 1
PPT
Software Design Patterns
PDF
Diving into VS 2015 Day1
PDF
Diving into VS 2015 Day4
PDF
Factory method pattern (Virtual Constructor)
PDF
C# 12 Pocket Reference: Instant Help for C# 12 Programmers Joseph Albahari
PPTX
Cble assignment powerpoint activity for moodle 1
Unit 2-Design Patterns.ppt
Design Pattern For C# Part 1
Software Design Patterns
Diving into VS 2015 Day1
Diving into VS 2015 Day4
Factory method pattern (Virtual Constructor)
C# 12 Pocket Reference: Instant Help for C# 12 Programmers Joseph Albahari
Cble assignment powerpoint activity for moodle 1

Similar to 03-Factory Method for design patterns.pdf (20)

DOCX
ASP.NET MVC3 RAD
DOCX
Patterns (contd)Software Development ProcessDesign patte.docx
DOCX
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
PPTX
Factory Method Pattern
PPTX
Factory Method Pattern
PDF
Design patterns 1july
PDF
MoDELS'16 presentation: Integration of a Graph-Based Model Indexer in Commerc...
PPTX
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
PDF
What’s new in Android: Embracing era of Generative AI
PPT
Vb introduction.
PPT
Overview of c#
PDF
Migrating From Cpp To C Sharp
PDF
Oops index
PDF
.NET Portfolio
PDF
File(2)
PDF
Diving into VS 2015 Day3
PPTX
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
PDF
Webinar on Design Patterns titled 'Dive into Design Patterns'
PDF
Diving into VS 2015 Day5
PDF
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
ASP.NET MVC3 RAD
Patterns (contd)Software Development ProcessDesign patte.docx
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
Factory Method Pattern
Factory Method Pattern
Design patterns 1july
MoDELS'16 presentation: Integration of a Graph-Based Model Indexer in Commerc...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
What’s new in Android: Embracing era of Generative AI
Vb introduction.
Overview of c#
Migrating From Cpp To C Sharp
Oops index
.NET Portfolio
File(2)
Diving into VS 2015 Day3
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Webinar on Design Patterns titled 'Dive into Design Patterns'
Diving into VS 2015 Day5
ChircuVictor StefircaMadalin rad_aspmvc3_wcf_vs2010
Ad

Recently uploaded (20)

DOCX
The story of the first moon landing.docx
PDF
Urban Design Final Project-Context
PPTX
12. Community Pharmacy and How to organize it
PDF
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
PPTX
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
PDF
The Advantages of Working With a Design-Build Studio
PDF
SEVA- Fashion designing-Presentation.pdf
PDF
Key Trends in Website Development 2025 | B3AITS - Bow & 3 Arrows IT Solutions
PPTX
LITERATURE CASE STUDY DESIGN SEMESTER 5.pptx
PPTX
HPE Aruba-master-icon-library_052722.pptx
PPT
UNIT I- Yarn, types, explanation, process
PPT
EGWHermeneuticsffgggggggggggggggggggggggggggggggg.ppt
PPTX
Entrepreneur intro, origin, process, method
PPT
pump pump is a mechanism that is used to transfer a liquid from one place to ...
PDF
BRANDBOOK-Presidential Award Scheme-Kenya-2023
PPTX
mahatma gandhi bus terminal in india Case Study.pptx
PDF
Quality Control Management for RMG, Level- 4, Certificate
PPTX
CLASS_11_BUSINESS_STUDIES_PPT_CHAPTER_1_Business_Trade_Commerce.pptx
PPTX
Special finishes, classification and types, explanation
DOCX
actividad 20% informatica microsoft project
The story of the first moon landing.docx
Urban Design Final Project-Context
12. Community Pharmacy and How to organize it
Integrated-2D-and-3D-Animation-Bridging-Dimensions-for-Impactful-Storytelling...
AC-Unit1.pptx CRYPTOGRAPHIC NNNNFOR ALL
The Advantages of Working With a Design-Build Studio
SEVA- Fashion designing-Presentation.pdf
Key Trends in Website Development 2025 | B3AITS - Bow & 3 Arrows IT Solutions
LITERATURE CASE STUDY DESIGN SEMESTER 5.pptx
HPE Aruba-master-icon-library_052722.pptx
UNIT I- Yarn, types, explanation, process
EGWHermeneuticsffgggggggggggggggggggggggggggggggg.ppt
Entrepreneur intro, origin, process, method
pump pump is a mechanism that is used to transfer a liquid from one place to ...
BRANDBOOK-Presidential Award Scheme-Kenya-2023
mahatma gandhi bus terminal in india Case Study.pptx
Quality Control Management for RMG, Level- 4, Certificate
CLASS_11_BUSINESS_STUDIES_PPT_CHAPTER_1_Business_Trade_Commerce.pptx
Special finishes, classification and types, explanation
actividad 20% informatica microsoft project
Ad

03-Factory Method for design patterns.pdf

  • 1. Design Patterns CSE354 Dr. Muhammad Farhan Assistant Professor, CUI Sahiwal farhan@cuisahiwal.edu.pk +92-3324428802
  • 3. Factory Method Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
  • 4. Problem (Cont…) Imagine that you’re creating a logistics management application. The first version of your app can only handle transportation by trucks, so the bulk of your code lives inside the Truck class. After a while, your app becomes pretty popular. Each day you receive dozens of requests from sea transportation companies to incorporate sea logistics into the app.
  • 5. Adding a new class to the program isn’t that simple if the rest of the code is already coupled to existing classes
  • 6. Problem Great news, right? But how about the code? At present, most of your code is coupled to the Truck class. Adding Ships into the app would require making changes to the entire codebase. Moreover, if later you decide to add another type of transportation to the app, you will probably need to make all of these changes again. As a result, you will end up with pretty nasty code, riddled with conditionals that switch the app’s behavior depending on the class of transportation objects.
  • 7. Solution (Cont…) The Factory Method pattern suggests that you replace direct object construction calls (using the new operator) with calls to a special factory method. Don’t worry: the objects are still created via the new operator, but it’s being called from within the factory method. Objects returned by a factory method are often referred to as “products.
  • 8. Solution (Cont…) At first glance, this change may look pointless: we just moved the constructor call from one part of the program to another. However, consider this: now you can override the factory method in a subclass and change the class of products being created by the method. There’s a slight limitation though: subclasses may return different types of products only if these products have a common base class or interface. Also, the factory method in the base class should have its return type declared as this interface.
  • 9. Solution (Cont...) For example, both Truck and Ship classes should implement the Transport interface, which declares a method called deliver . Each class implements this method differently: trucks deliver cargo by land; ships deliver cargo by sea. The factory method in the RoadLogistics class returns truck objects, whereas the factory method in the SeaLogistics class returns ships.
  • 10. Solution (Cont…) The code that uses the factory method (often called the client code) doesn’t see a difference between the actual products returned by various subclasses. The client treats all the products as abstract Transport .
  • 11. Solution The client knows that all transport objects are supposed to have the deliver method, but exactly how it works isn’t important to the client.
  • 13. Structure (Cont…) The Product declares the interface, which is common to all objects that can be produced by the creator and its subclasses. Concrete Products are different implementations of the product interface. The Creator class declares the factory method that returns new product objects. It’s important that the return type of this method matches the product interface. You can declare the factory method as abstract to force all subclasses to implement their own versions of the method. As an alternative, the base factory method can return some default product type
  • 14. Structure (Cont…) Note, despite its name, product creation is not the primary responsibility of the creator. Usually, the creator class already has some core business logic related to products. The factory method helps to decouple this logic from the concrete product classes. Here is an analogy: a large software development company can have a training department for programmers. However, the primary function of the company as a whole is still writing code, not producing programmers.
  • 15. Structure Concrete Creators override the base factory method, so it returns a different type of product. 76 Creational Design Patterns / Factory Method Note that the factory method doesn’t have to create new instances all the time. It can also return existing objects from a cache, an object pool, or another source.