SlideShare a Scribd company logo
Factory Pattern
Factory Pattern
Duck duck;
if (picnic) {
duck = new MallardDuck();
} else if (hunting) {
duck = new DecoyDuck();
} else if (inBathTub) {
duck = new RubberDuck();
}
Factory Pattern(Contd.)
Pizza orderPizza(String type) {
Pizza pizza;
if (type.equals(“cheese”)) {
pizza = new CheesePizza();
}
else if (type.equals(“greek”) {
pizza = new GreekPizza();
}
else if (type.equals(“pepperoni”)
{
pizza = new PepperoniPizza();
}
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
Factory Pattern (Contd.)
public class SimplePizzaFactory {
public Pizza createPizza(String type) {
Pizza pizza = null;
if (type.equals(“cheese”)) {
pizza = new CheesePizza();
} else if (type.equals(“pepperoni”)) {
pizza = new PepperoniPizza();
}
else if (type.equals(“clam”)) {
pizza = new ClamPizza();
}
else if (type.equals(“veggie”)) {
pizza = new VeggiePizza();
}
return pizza;
}
}
Factory Pattern (Contd.)
public class PizzaStore {
SimplePizzaFactory factory;
public PizzaStore(SimplePizzaFactory
factory) {
this.factory = factory;
}
public Pizza orderPizza(String type) {
Pizza pizza;
pizza = factory.createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
// other methods here
}
Factory Pattern(Contd.)
NYPizzaFactory nyFactory = new NYPizzaFactory();
PizzaStore nyStore = new PizzaStore(nyFactory);
nyStore.order(“Veggie”);
ChicagoPizzaFactory chicagoFactory = new ChicagoPizzaFactory();
PizzaStore chicagoStore = new PizzaStore(chicagoFactory);
chicagoStore.order(“Veggie”);
Factory Pattern(Contd.)
public abstract class PizzaStore {
public Pizza orderPizza(String type) {
Pizza pizza;
pizza = createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
protected abstract Pizza createPizza(String type);
}
Factory Pattern(Contd.)
public class NYStylePizzaStore extends PizzaStore {
public Pizza createPizza(type) {
if (type.equals(“cheese”)) {
pizza = new NYStyleCheesePizza();
} else if (type.equals(“pepperoni”) {
pizza = new NYStylePepperoniPizza();
} else if (type.equals(“clam”) {
pizza = new NYStyleClamPizza();
} else if (type.equals(“veggie”) {
pizza = new NYStyleVeggiePizza();
}
}
}
Factory Pattern(Contd.)
public class ChicagoStylePizzaStore extends PizzaStore {
public Pizza createPizza(type) {
if (type.equals(“cheese”)) {
pizza = new ChicagoStyleCheesePizza();
} else if (type.equals(“pepperoni”) {
pizza = new ChicagoStylePepperoniPizza();
} else if (type.equals(“clam”) {
pizza = new ChicagoStyleClamPizza();
} else if (type.equals(“veggie”) {
pizza = new ChicagoStyleVeggiePizza();
}
}
}
Factory Pattern (Contd.)
public abstract class Pizza {
String name;
String dough;
String sauce;
ArrayList<String> toppings = new
ArrayList<String>();
void prepare() {
System.out.println(name);
System.out.println(“dough...”);
System.out.println(“sauce...”);
System.out.println(“toppings:“);
for (int i = 0; i < toppings.size(); i++)
{
System.out.println(“ “ +
toppings.get(i));
}
}
void bake() {
System.out.println(“Bake”);
}
void cut() {
System.out.println(“Cutting slices”);
}
void box() {
System.out.println(“box”);
}
public String getName() {
return name;
}
}
Factory Pattern(Contd.)
public class NYStyleCheesePizza extends
Pizza {
public NYStyleCheesePizza() {
name = “NY Style SCheese Pizza”;
dough = “Thin Crust Dough”;
sauce = “Marinara Sauce”;
toppings.add(“Grated Reggiano
Cheese”);
}
}
public class ChicagoStyleCheesePizza
extends Pizza {
public ChicagoStyleCheesePizza() {
name = “Chicago Cheese Pizza”;
dough = “Extra Thick Crust Dough”;
sauce = “Plum Tomato Sauce”;
toppings.add(“Cheese”);
}
void cut() {
System.out.println(“Cutting slices”);
}
}
Factory Pattern(Contd.)
public class PizzaTestDrive {
public static void main(String[] args) {
PizzaStore nyStore = new NYPizzaStore();
PizzaStore chicagoStore = new ChicagoPizzaStore();
Pizza pizza = nyStore.orderPizza(“cheese”);
System.out.println(“Ethan ordered a “ + pizza.getName() + “n”);
pizza = chicagoStore.orderPizza(“cheese”);
System.out.println(“Joel ordered a “ + pizza.getName() + “n”);
}
}
Factory Pattern(Contd.)
● The Factory Method Pattern defines an interface for
creating an object, but lets subclasses decide which
class to instantiate. Factory Method lets a class
defer instantiation to subclasses.
● It also helps to relate to parallel class hierarchies
(the Product and the Creator).
Dependency Inversion Principle
● Depend upon abstractions. Do not depend upon
concrete classes.
● A successor of this principle is Inversion of Control,
also known as Hollywood Principle (IoC in short).
● A successor of IoC is Dependency Injection
Principle.
Dependency Inversion Guidelines
● No variable should hold a reference to a concrete
class.
● No class should derive from a concrete class.
● No method should override an implemented method
of any of its base classes.
Resources
● Head First Design Pattern
Thank You
Ad

More Related Content

Recently uploaded (20)

mr discrimination________________________________________________________.pdf
mr discrimination________________________________________________________.pdfmr discrimination________________________________________________________.pdf
mr discrimination________________________________________________________.pdf
Leonid Ledata
 
The Psychology of Scene Transitions - Mark Murphy Director
The Psychology of Scene Transitions - Mark Murphy DirectorThe Psychology of Scene Transitions - Mark Murphy Director
The Psychology of Scene Transitions - Mark Murphy Director
Mark Murphy Director
 
Adapting to Change_ How Los Angeles' TV Industry is Evolving by David Shane P...
Adapting to Change_ How Los Angeles' TV Industry is Evolving by David Shane P...Adapting to Change_ How Los Angeles' TV Industry is Evolving by David Shane P...
Adapting to Change_ How Los Angeles' TV Industry is Evolving by David Shane P...
David Shane PR
 
Talentsskskaskkakakakak Aquisition OEC108.pptx
Talentsskskaskkakakakak Aquisition OEC108.pptxTalentsskskaskkakakakak Aquisition OEC108.pptx
Talentsskskaskkakakakak Aquisition OEC108.pptx
sriyansh4443
 
BuzzerSaarang IITM Chennai Finals Quiz.pptx
BuzzerSaarang IITM Chennai Finals Quiz.pptxBuzzerSaarang IITM Chennai Finals Quiz.pptx
BuzzerSaarang IITM Chennai Finals Quiz.pptx
gabssienna0o
 
LESSON 7_Basic metal fabrication techniques (Cutting, Shaping, Welding).pptx
LESSON 7_Basic metal fabrication techniques (Cutting, Shaping, Welding).pptxLESSON 7_Basic metal fabrication techniques (Cutting, Shaping, Welding).pptx
LESSON 7_Basic metal fabrication techniques (Cutting, Shaping, Welding).pptx
jojiesalgado
 
Understanding Rich Messaging Services Enhancing Communication in the Digital ...
Understanding Rich Messaging Services Enhancing Communication in the Digital ...Understanding Rich Messaging Services Enhancing Communication in the Digital ...
Understanding Rich Messaging Services Enhancing Communication in the Digital ...
Times Mobile
 
mr discrimination________________________________________________________1111...
mr discrimination________________________________________________________1111...mr discrimination________________________________________________________1111...
mr discrimination________________________________________________________1111...
Leonid Ledata
 
A Hypothetical ad for ether a Video Game, TV show, Movie.
A Hypothetical ad for ether a Video Game, TV show,  Movie.A Hypothetical ad for ether a Video Game, TV show,  Movie.
A Hypothetical ad for ether a Video Game, TV show, Movie.
skylarleakeybusiness
 
Prelims Saarang IITM Chennai Finals+ Answers.pptx
Prelims Saarang IITM Chennai Finals+ Answers.pptxPrelims Saarang IITM Chennai Finals+ Answers.pptx
Prelims Saarang IITM Chennai Finals+ Answers.pptx
gabssienna0o
 
A Boy is being left-out because he has no Girlfriends due to his low performa...
A Boy is being left-out because he has no Girlfriends due to his low performa...A Boy is being left-out because he has no Girlfriends due to his low performa...
A Boy is being left-out because he has no Girlfriends due to his low performa...
Laxu _00
 
Millie Bobby Brown Life Biography The Celeb Post
Millie Bobby Brown Life Biography The Celeb PostMillie Bobby Brown Life Biography The Celeb Post
Millie Bobby Brown Life Biography The Celeb Post
Lionapk
 
Pension-Rules-1-2021040512 0919.ppt
Pension-Rules-1-2021040512      0919.pptPension-Rules-1-2021040512      0919.ppt
Pension-Rules-1-2021040512 0919.ppt
SwathyKrishna55
 
ch11.pptKGYUTFYDRERLJIOUY7T867RVHOJIP09-IU08Y7GTFGYU890-I90UIYGUI
ch11.pptKGYUTFYDRERLJIOUY7T867RVHOJIP09-IU08Y7GTFGYU890-I90UIYGUIch11.pptKGYUTFYDRERLJIOUY7T867RVHOJIP09-IU08Y7GTFGYU890-I90UIYGUI
ch11.pptKGYUTFYDRERLJIOUY7T867RVHOJIP09-IU08Y7GTFGYU890-I90UIYGUI
Abodahab
 
mr discrimination________________________________________________________111.pdf
mr discrimination________________________________________________________111.pdfmr discrimination________________________________________________________111.pdf
mr discrimination________________________________________________________111.pdf
Leonid Ledata
 
西班牙阿利坎特大学毕业证书留信网认证UA成绩单办本科学位证
西班牙阿利坎特大学毕业证书留信网认证UA成绩单办本科学位证西班牙阿利坎特大学毕业证书留信网认证UA成绩单办本科学位证
西班牙阿利坎特大学毕业证书留信网认证UA成绩单办本科学位证
taqyea
 
10 Best IPTV Free Trials in 2025 (Try Before You Buy).pdf
10 Best IPTV Free Trials in 2025 (Try Before You Buy).pdf10 Best IPTV Free Trials in 2025 (Try Before You Buy).pdf
10 Best IPTV Free Trials in 2025 (Try Before You Buy).pdf
BEST IPTV
 
Travis Kelce Life Biography The Celeb Post
Travis Kelce Life Biography The Celeb PostTravis Kelce Life Biography The Celeb Post
Travis Kelce Life Biography The Celeb Post
Lionapk
 
Movement intervention in sepecial population children .pptx
Movement intervention in sepecial population children .pptxMovement intervention in sepecial population children .pptx
Movement intervention in sepecial population children .pptx
nakisanianeani
 
mr discrimination________________________________________________________1111...
mr discrimination________________________________________________________1111...mr discrimination________________________________________________________1111...
mr discrimination________________________________________________________1111...
Leonid Ledata
 
mr discrimination________________________________________________________.pdf
mr discrimination________________________________________________________.pdfmr discrimination________________________________________________________.pdf
mr discrimination________________________________________________________.pdf
Leonid Ledata
 
The Psychology of Scene Transitions - Mark Murphy Director
The Psychology of Scene Transitions - Mark Murphy DirectorThe Psychology of Scene Transitions - Mark Murphy Director
The Psychology of Scene Transitions - Mark Murphy Director
Mark Murphy Director
 
Adapting to Change_ How Los Angeles' TV Industry is Evolving by David Shane P...
Adapting to Change_ How Los Angeles' TV Industry is Evolving by David Shane P...Adapting to Change_ How Los Angeles' TV Industry is Evolving by David Shane P...
Adapting to Change_ How Los Angeles' TV Industry is Evolving by David Shane P...
David Shane PR
 
Talentsskskaskkakakakak Aquisition OEC108.pptx
Talentsskskaskkakakakak Aquisition OEC108.pptxTalentsskskaskkakakakak Aquisition OEC108.pptx
Talentsskskaskkakakakak Aquisition OEC108.pptx
sriyansh4443
 
BuzzerSaarang IITM Chennai Finals Quiz.pptx
BuzzerSaarang IITM Chennai Finals Quiz.pptxBuzzerSaarang IITM Chennai Finals Quiz.pptx
BuzzerSaarang IITM Chennai Finals Quiz.pptx
gabssienna0o
 
LESSON 7_Basic metal fabrication techniques (Cutting, Shaping, Welding).pptx
LESSON 7_Basic metal fabrication techniques (Cutting, Shaping, Welding).pptxLESSON 7_Basic metal fabrication techniques (Cutting, Shaping, Welding).pptx
LESSON 7_Basic metal fabrication techniques (Cutting, Shaping, Welding).pptx
jojiesalgado
 
Understanding Rich Messaging Services Enhancing Communication in the Digital ...
Understanding Rich Messaging Services Enhancing Communication in the Digital ...Understanding Rich Messaging Services Enhancing Communication in the Digital ...
Understanding Rich Messaging Services Enhancing Communication in the Digital ...
Times Mobile
 
mr discrimination________________________________________________________1111...
mr discrimination________________________________________________________1111...mr discrimination________________________________________________________1111...
mr discrimination________________________________________________________1111...
Leonid Ledata
 
A Hypothetical ad for ether a Video Game, TV show, Movie.
A Hypothetical ad for ether a Video Game, TV show,  Movie.A Hypothetical ad for ether a Video Game, TV show,  Movie.
A Hypothetical ad for ether a Video Game, TV show, Movie.
skylarleakeybusiness
 
Prelims Saarang IITM Chennai Finals+ Answers.pptx
Prelims Saarang IITM Chennai Finals+ Answers.pptxPrelims Saarang IITM Chennai Finals+ Answers.pptx
Prelims Saarang IITM Chennai Finals+ Answers.pptx
gabssienna0o
 
A Boy is being left-out because he has no Girlfriends due to his low performa...
A Boy is being left-out because he has no Girlfriends due to his low performa...A Boy is being left-out because he has no Girlfriends due to his low performa...
A Boy is being left-out because he has no Girlfriends due to his low performa...
Laxu _00
 
Millie Bobby Brown Life Biography The Celeb Post
Millie Bobby Brown Life Biography The Celeb PostMillie Bobby Brown Life Biography The Celeb Post
Millie Bobby Brown Life Biography The Celeb Post
Lionapk
 
Pension-Rules-1-2021040512 0919.ppt
Pension-Rules-1-2021040512      0919.pptPension-Rules-1-2021040512      0919.ppt
Pension-Rules-1-2021040512 0919.ppt
SwathyKrishna55
 
ch11.pptKGYUTFYDRERLJIOUY7T867RVHOJIP09-IU08Y7GTFGYU890-I90UIYGUI
ch11.pptKGYUTFYDRERLJIOUY7T867RVHOJIP09-IU08Y7GTFGYU890-I90UIYGUIch11.pptKGYUTFYDRERLJIOUY7T867RVHOJIP09-IU08Y7GTFGYU890-I90UIYGUI
ch11.pptKGYUTFYDRERLJIOUY7T867RVHOJIP09-IU08Y7GTFGYU890-I90UIYGUI
Abodahab
 
mr discrimination________________________________________________________111.pdf
mr discrimination________________________________________________________111.pdfmr discrimination________________________________________________________111.pdf
mr discrimination________________________________________________________111.pdf
Leonid Ledata
 
西班牙阿利坎特大学毕业证书留信网认证UA成绩单办本科学位证
西班牙阿利坎特大学毕业证书留信网认证UA成绩单办本科学位证西班牙阿利坎特大学毕业证书留信网认证UA成绩单办本科学位证
西班牙阿利坎特大学毕业证书留信网认证UA成绩单办本科学位证
taqyea
 
10 Best IPTV Free Trials in 2025 (Try Before You Buy).pdf
10 Best IPTV Free Trials in 2025 (Try Before You Buy).pdf10 Best IPTV Free Trials in 2025 (Try Before You Buy).pdf
10 Best IPTV Free Trials in 2025 (Try Before You Buy).pdf
BEST IPTV
 
Travis Kelce Life Biography The Celeb Post
Travis Kelce Life Biography The Celeb PostTravis Kelce Life Biography The Celeb Post
Travis Kelce Life Biography The Celeb Post
Lionapk
 
Movement intervention in sepecial population children .pptx
Movement intervention in sepecial population children .pptxMovement intervention in sepecial population children .pptx
Movement intervention in sepecial population children .pptx
nakisanianeani
 
mr discrimination________________________________________________________1111...
mr discrimination________________________________________________________1111...mr discrimination________________________________________________________1111...
mr discrimination________________________________________________________1111...
Leonid Ledata
 

Featured (20)

2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
 
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
 
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
 
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
 
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing2024 Trend Updates: What Really Works In SEO & Content Marketing
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
 
Storytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design ProcessStorytelling For The Web: Integrate Storytelling in your Design Process
Storytelling For The Web: Integrate Storytelling in your Design Process
Chiara Aliotta
 
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
 
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
Ad

Factory Method Pattern

  • 2. Factory Pattern Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new DecoyDuck(); } else if (inBathTub) { duck = new RubberDuck(); }
  • 3. Factory Pattern(Contd.) Pizza orderPizza(String type) { Pizza pizza; if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if (type.equals(“greek”) { pizza = new GreekPizza(); } else if (type.equals(“pepperoni”) { pizza = new PepperoniPizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }
  • 4. Factory Pattern (Contd.) public class SimplePizzaFactory { public Pizza createPizza(String type) { Pizza pizza = null; if (type.equals(“cheese”)) { pizza = new CheesePizza(); } else if (type.equals(“pepperoni”)) { pizza = new PepperoniPizza(); } else if (type.equals(“clam”)) { pizza = new ClamPizza(); } else if (type.equals(“veggie”)) { pizza = new VeggiePizza(); } return pizza; } }
  • 5. Factory Pattern (Contd.) public class PizzaStore { SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory) { this.factory = factory; } public Pizza orderPizza(String type) { Pizza pizza; pizza = factory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } // other methods here }
  • 6. Factory Pattern(Contd.) NYPizzaFactory nyFactory = new NYPizzaFactory(); PizzaStore nyStore = new PizzaStore(nyFactory); nyStore.order(“Veggie”); ChicagoPizzaFactory chicagoFactory = new ChicagoPizzaFactory(); PizzaStore chicagoStore = new PizzaStore(chicagoFactory); chicagoStore.order(“Veggie”);
  • 7. Factory Pattern(Contd.) public abstract class PizzaStore { public Pizza orderPizza(String type) { Pizza pizza; pizza = createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } protected abstract Pizza createPizza(String type); }
  • 8. Factory Pattern(Contd.) public class NYStylePizzaStore extends PizzaStore { public Pizza createPizza(type) { if (type.equals(“cheese”)) { pizza = new NYStyleCheesePizza(); } else if (type.equals(“pepperoni”) { pizza = new NYStylePepperoniPizza(); } else if (type.equals(“clam”) { pizza = new NYStyleClamPizza(); } else if (type.equals(“veggie”) { pizza = new NYStyleVeggiePizza(); } } }
  • 9. Factory Pattern(Contd.) public class ChicagoStylePizzaStore extends PizzaStore { public Pizza createPizza(type) { if (type.equals(“cheese”)) { pizza = new ChicagoStyleCheesePizza(); } else if (type.equals(“pepperoni”) { pizza = new ChicagoStylePepperoniPizza(); } else if (type.equals(“clam”) { pizza = new ChicagoStyleClamPizza(); } else if (type.equals(“veggie”) { pizza = new ChicagoStyleVeggiePizza(); } } }
  • 10. Factory Pattern (Contd.) public abstract class Pizza { String name; String dough; String sauce; ArrayList<String> toppings = new ArrayList<String>(); void prepare() { System.out.println(name); System.out.println(“dough...”); System.out.println(“sauce...”); System.out.println(“toppings:“); for (int i = 0; i < toppings.size(); i++) { System.out.println(“ “ + toppings.get(i)); } } void bake() { System.out.println(“Bake”); } void cut() { System.out.println(“Cutting slices”); } void box() { System.out.println(“box”); } public String getName() { return name; } }
  • 11. Factory Pattern(Contd.) public class NYStyleCheesePizza extends Pizza { public NYStyleCheesePizza() { name = “NY Style SCheese Pizza”; dough = “Thin Crust Dough”; sauce = “Marinara Sauce”; toppings.add(“Grated Reggiano Cheese”); } } public class ChicagoStyleCheesePizza extends Pizza { public ChicagoStyleCheesePizza() { name = “Chicago Cheese Pizza”; dough = “Extra Thick Crust Dough”; sauce = “Plum Tomato Sauce”; toppings.add(“Cheese”); } void cut() { System.out.println(“Cutting slices”); } }
  • 12. Factory Pattern(Contd.) public class PizzaTestDrive { public static void main(String[] args) { PizzaStore nyStore = new NYPizzaStore(); PizzaStore chicagoStore = new ChicagoPizzaStore(); Pizza pizza = nyStore.orderPizza(“cheese”); System.out.println(“Ethan ordered a “ + pizza.getName() + “n”); pizza = chicagoStore.orderPizza(“cheese”); System.out.println(“Joel ordered a “ + pizza.getName() + “n”); } }
  • 13. Factory Pattern(Contd.) ● The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. ● It also helps to relate to parallel class hierarchies (the Product and the Creator).
  • 14. Dependency Inversion Principle ● Depend upon abstractions. Do not depend upon concrete classes. ● A successor of this principle is Inversion of Control, also known as Hollywood Principle (IoC in short). ● A successor of IoC is Dependency Injection Principle.
  • 15. Dependency Inversion Guidelines ● No variable should hold a reference to a concrete class. ● No class should derive from a concrete class. ● No method should override an implemented method of any of its base classes.
  • 16. Resources ● Head First Design Pattern