SlideShare a Scribd company logo
1 of 8
Simple Factory Design Pattern With
RIP
1
Arifur Rahman
Software Engineer
Brain Station-23
What is factory pattern ?
The factory pattern is a popularly used design pattern and it is very useful to restrict clients
from knowing the actual business logic methods, it is useful to create a decoupled system,
and it is useful to eliminate object creation in a client environment.
Intent :
1. Creates objects without exposing the instantiation logic to the client.
2. Refers to the newly created object through a common interface.
2
Problem :
For example we have a scenario where based on user input we need to call particular class methods.
//
// customer enters their choice
//
If (choice == "Car")
{
// call car class and it’s methods
Car c = new Car();
c.buy();
}
If (choice == "bike")
{
// call bike class and it’s methods
Bike b = new Bike();
b.buy();
}
3
Factory pattern
public enum ChoiceType : int
{
Bike = 1,
Car
}
// Interface
public interface IChoice
{
string Buy();
}
public class clsBike : IChoice
{
public string Buy(){
return ("You choose Bike");
}
}
public class clsCar : IChoice
{
public string Buy(){
return ("You choose Car");
}
}
4
Factory pattern contd.
// Factory Class
public class FactoryChoice
{
static public IChoice getChoiceObj(ChoiceType choice)
{
IChoice objChoice = null;
if (choice == ChoiceType.Car)
{
objChoice = new clsCar();
}
else if (choice == ChoiceType.Bike)
{
objChoice = new clsBike();
}
return objChoice;
}
}
5
RIP (Replace IF with Polymorphism)
public class FactoryChoice
{
private IDictionary<ChoiceType, IChoice> _choices;
public FactoryChoice()
{
_choices = new Dictionary<ChoiceType, IChoice>
{
{ChoiceType.Bike, new clsBike() },
{ChoiceType.Car, new clsCar()}
};
}
static public IChoice getChoiceObj(ChoiceType choice)
{
var factory = new FactoryChoice();
return factory._choices[choice];
}
}
6
Factory pattern contd.
class Program
{
static void Main(string[] args)
{
//Client class
IChoice objInvoice;
objInvoice = FactoryChoice.getChoiceObj(ChoiceType.Bike);
Console.Write(objInvoice.Buy());
Console.ReadKey();
}
}
7
8

More Related Content

Viewers also liked

Architecture and Industrial revolution
Architecture and Industrial revolutionArchitecture and Industrial revolution
Architecture and Industrial revolutionBhartendu Vimal
 
Definition of industry
Definition of industryDefinition of industry
Definition of industryMandeep Sidhu
 
Types Of Manufacturing Industry
Types Of Manufacturing IndustryTypes Of Manufacturing Industry
Types Of Manufacturing IndustryJohn Barlow
 
WebApps e Frameworks Javascript
WebApps e Frameworks JavascriptWebApps e Frameworks Javascript
WebApps e Frameworks Javascriptmeet2Brains
 
Industrial design [compatibility mode]
Industrial design [compatibility mode]Industrial design [compatibility mode]
Industrial design [compatibility mode]Delwin Arikatt
 
Industrial Design
Industrial DesignIndustrial Design
Industrial DesignNIELITA
 
Manufacturing industries class 10
Manufacturing industries class 10Manufacturing industries class 10
Manufacturing industries class 10Santosh Kumar
 
Industries for class 8
Industries for class 8Industries for class 8
Industries for class 8LearnX365
 
Impact of industrial revolution in architecture
Impact of industrial revolution in architectureImpact of industrial revolution in architecture
Impact of industrial revolution in architectureShabna Azeez
 
architecture history - Industrial revolution
architecture history - Industrial revolutionarchitecture history - Industrial revolution
architecture history - Industrial revolutionomarnene
 
Industrial Revolution (Architectural)
Industrial Revolution (Architectural)Industrial Revolution (Architectural)
Industrial Revolution (Architectural)Sahil Kaundal
 
3 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 20173 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 2017Drift
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheLeslie Samuel
 

Viewers also liked (18)

Architecture and Industrial revolution
Architecture and Industrial revolutionArchitecture and Industrial revolution
Architecture and Industrial revolution
 
Definition of industry
Definition of industryDefinition of industry
Definition of industry
 
Types Of Manufacturing Industry
Types Of Manufacturing IndustryTypes Of Manufacturing Industry
Types Of Manufacturing Industry
 
WebApps e Frameworks Javascript
WebApps e Frameworks JavascriptWebApps e Frameworks Javascript
WebApps e Frameworks Javascript
 
Industries
IndustriesIndustries
Industries
 
Industrial design [compatibility mode]
Industrial design [compatibility mode]Industrial design [compatibility mode]
Industrial design [compatibility mode]
 
Types of industry
Types of industry Types of industry
Types of industry
 
Industrial Design
Industrial DesignIndustrial Design
Industrial Design
 
Industrial design
Industrial designIndustrial design
Industrial design
 
Manufacturing industries class 10
Manufacturing industries class 10Manufacturing industries class 10
Manufacturing industries class 10
 
Plastic roads
Plastic roadsPlastic roads
Plastic roads
 
Industries for class 8
Industries for class 8Industries for class 8
Industries for class 8
 
Manufacturing industries
Manufacturing industriesManufacturing industries
Manufacturing industries
 
Impact of industrial revolution in architecture
Impact of industrial revolution in architectureImpact of industrial revolution in architecture
Impact of industrial revolution in architecture
 
architecture history - Industrial revolution
architecture history - Industrial revolutionarchitecture history - Industrial revolution
architecture history - Industrial revolution
 
Industrial Revolution (Architectural)
Industrial Revolution (Architectural)Industrial Revolution (Architectural)
Industrial Revolution (Architectural)
 
3 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 20173 Things Every Sales Team Needs to Be Thinking About in 2017
3 Things Every Sales Team Needs to Be Thinking About in 2017
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
 

Similar to Factory pattern with rip

Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design PatternKanushka Gayan
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project D. j Vicky
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project D. j Vicky
 
Super market billing system using webcam
Super market billing system using webcam Super market billing system using webcam
Super market billing system using webcam SahithBeats
 
cbse 12 computer science IP
cbse 12 computer science IPcbse 12 computer science IP
cbse 12 computer science IPD. j Vicky
 
A CASE Lab Report - Project File on "ATM - Banking System"
A CASE Lab Report - Project File on  "ATM - Banking System"A CASE Lab Report - Project File on  "ATM - Banking System"
A CASE Lab Report - Project File on "ATM - Banking System"joyousbharat
 
Car Rental in C++
Car Rental in C++Car Rental in C++
Car Rental in C++RitickKamal
 
汽車協同平台簡報
汽車協同平台簡報汽車協同平台簡報
汽車協同平台簡報Jimmy Chang
 
GanttChart for ClearQuest Install Guide
GanttChart for ClearQuest Install GuideGanttChart for ClearQuest Install Guide
GanttChart for ClearQuest Install GuideAlexander Novichkov
 
Waterfall models.ppt
Waterfall models.pptWaterfall models.ppt
Waterfall models.pptPawanRaj48
 
Best Practices & Lessons Learned from the field on EMC Documentum xCP 2.0
Best Practices & Lessons Learned from the field on EMC Documentum xCP 2.0Best Practices & Lessons Learned from the field on EMC Documentum xCP 2.0
Best Practices & Lessons Learned from the field on EMC Documentum xCP 2.0Haytham Ghandour
 
CAD/CAM/CIM (18ME72) Module-5 Part-A
CAD/CAM/CIM  (18ME72) Module-5 Part-ACAD/CAM/CIM  (18ME72) Module-5 Part-A
CAD/CAM/CIM (18ME72) Module-5 Part-AMohammed Imran
 

Similar to Factory pattern with rip (20)

Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project
 
cbse 12 computer science investigatory project
cbse 12 computer science investigatory project  cbse 12 computer science investigatory project
cbse 12 computer science investigatory project
 
Super market billing system using webcam
Super market billing system using webcam Super market billing system using webcam
Super market billing system using webcam
 
cbse 12 computer science IP
cbse 12 computer science IPcbse 12 computer science IP
cbse 12 computer science IP
 
A CASE Lab Report - Project File on "ATM - Banking System"
A CASE Lab Report - Project File on  "ATM - Banking System"A CASE Lab Report - Project File on  "ATM - Banking System"
A CASE Lab Report - Project File on "ATM - Banking System"
 
Car Rental in C++
Car Rental in C++Car Rental in C++
Car Rental in C++
 
汽車協同平台簡報
汽車協同平台簡報汽車協同平台簡報
汽車協同平台簡報
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
GanttChart for ClearQuest Install Guide
GanttChart for ClearQuest Install GuideGanttChart for ClearQuest Install Guide
GanttChart for ClearQuest Install Guide
 
Foundations of programming
Foundations of programmingFoundations of programming
Foundations of programming
 
chapter 2 (1).ppt
chapter 2 (1).pptchapter 2 (1).ppt
chapter 2 (1).ppt
 
Waterfall models.ppt
Waterfall models.pptWaterfall models.ppt
Waterfall models.ppt
 
Model.ppt
Model.pptModel.ppt
Model.ppt
 
Best Practices & Lessons Learned from the field on EMC Documentum xCP 2.0
Best Practices & Lessons Learned from the field on EMC Documentum xCP 2.0Best Practices & Lessons Learned from the field on EMC Documentum xCP 2.0
Best Practices & Lessons Learned from the field on EMC Documentum xCP 2.0
 
The MirAL Story
The MirAL StoryThe MirAL Story
The MirAL Story
 
CAD/CAM/CIM (18ME72) Module-5 Part-A
CAD/CAM/CIM  (18ME72) Module-5 Part-ACAD/CAM/CIM  (18ME72) Module-5 Part-A
CAD/CAM/CIM (18ME72) Module-5 Part-A
 

Recently uploaded

Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Anthony Dahanne
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profileakrivarotava
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 

Recently uploaded (20)

Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profile
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 

Factory pattern with rip

  • 1. Simple Factory Design Pattern With RIP 1 Arifur Rahman Software Engineer Brain Station-23
  • 2. What is factory pattern ? The factory pattern is a popularly used design pattern and it is very useful to restrict clients from knowing the actual business logic methods, it is useful to create a decoupled system, and it is useful to eliminate object creation in a client environment. Intent : 1. Creates objects without exposing the instantiation logic to the client. 2. Refers to the newly created object through a common interface. 2
  • 3. Problem : For example we have a scenario where based on user input we need to call particular class methods. // // customer enters their choice // If (choice == "Car") { // call car class and it’s methods Car c = new Car(); c.buy(); } If (choice == "bike") { // call bike class and it’s methods Bike b = new Bike(); b.buy(); } 3
  • 4. Factory pattern public enum ChoiceType : int { Bike = 1, Car } // Interface public interface IChoice { string Buy(); } public class clsBike : IChoice { public string Buy(){ return ("You choose Bike"); } } public class clsCar : IChoice { public string Buy(){ return ("You choose Car"); } } 4
  • 5. Factory pattern contd. // Factory Class public class FactoryChoice { static public IChoice getChoiceObj(ChoiceType choice) { IChoice objChoice = null; if (choice == ChoiceType.Car) { objChoice = new clsCar(); } else if (choice == ChoiceType.Bike) { objChoice = new clsBike(); } return objChoice; } } 5
  • 6. RIP (Replace IF with Polymorphism) public class FactoryChoice { private IDictionary<ChoiceType, IChoice> _choices; public FactoryChoice() { _choices = new Dictionary<ChoiceType, IChoice> { {ChoiceType.Bike, new clsBike() }, {ChoiceType.Car, new clsCar()} }; } static public IChoice getChoiceObj(ChoiceType choice) { var factory = new FactoryChoice(); return factory._choices[choice]; } } 6
  • 7. Factory pattern contd. class Program { static void Main(string[] args) { //Client class IChoice objInvoice; objInvoice = FactoryChoice.getChoiceObj(ChoiceType.Bike); Console.Write(objInvoice.Buy()); Console.ReadKey(); } } 7
  • 8. 8