SlideShare ist ein Scribd-Unternehmen logo
1 von 17
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

Weitere ähnliche Inhalte

Mehr von MD Sayem Ahmed

Distributed systems - A Primer
Distributed systems - A PrimerDistributed systems - A Primer
Distributed systems - A PrimerMD Sayem Ahmed
 
An Introduction to Maven Part 1
An Introduction to Maven Part 1An Introduction to Maven Part 1
An Introduction to Maven Part 1MD Sayem Ahmed
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworksMD Sayem Ahmed
 
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascriptMD Sayem Ahmed
 

Mehr von MD Sayem Ahmed (6)

Distributed systems - A Primer
Distributed systems - A PrimerDistributed systems - A Primer
Distributed systems - A Primer
 
An Introduction to Maven Part 1
An Introduction to Maven Part 1An Introduction to Maven Part 1
An Introduction to Maven Part 1
 
A brief overview of java frameworks
A brief overview of java frameworksA brief overview of java frameworks
A brief overview of java frameworks
 
Restful web services
Restful web servicesRestful web services
Restful web services
 
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascript
 
01. design pattern
01. design pattern01. design pattern
01. design pattern
 

Kürzlich hochgeladen

VIP ( Goa Call Girls ) Margao Beach👉 8617370543 Escort Service Enjoy Your Dre...
VIP ( Goa Call Girls ) Margao Beach👉 8617370543 Escort Service Enjoy Your Dre...VIP ( Goa Call Girls ) Margao Beach👉 8617370543 Escort Service Enjoy Your Dre...
VIP ( Goa Call Girls ) Margao Beach👉 8617370543 Escort Service Enjoy Your Dre...Nitya salvi
 
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...rahim quresi
 
Dum Dum ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready...
Dum Dum ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready...Dum Dum ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready...
Dum Dum ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready...ritikasharma
 
📞 Contact Number 8617370543VIP Rajsamand Call Girls
📞 Contact Number 8617370543VIP Rajsamand Call Girls📞 Contact Number 8617370543VIP Rajsamand Call Girls
📞 Contact Number 8617370543VIP Rajsamand Call GirlsNitya salvi
 
Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...
Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...
Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...ritikasharma
 
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRLBhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRLNitya salvi
 
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034 Independe...
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034  Independe...Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034  Independe...
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034 Independe... Shivani Pandey
 
VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...
VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...
VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...SUHANI PANDEY
 
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call GirlsNitya salvi
 
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...rajveermohali2022
 
WhatsApp Chat: 📞 8617697112 Hire Call Girls Raiganj For a Sensual Sex Experience
WhatsApp Chat: 📞 8617697112 Hire Call Girls Raiganj For a Sensual Sex ExperienceWhatsApp Chat: 📞 8617697112 Hire Call Girls Raiganj For a Sensual Sex Experience
WhatsApp Chat: 📞 8617697112 Hire Call Girls Raiganj For a Sensual Sex ExperienceNitya salvi
 
Navsari Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girl...
Navsari Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girl...Navsari Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girl...
Navsari Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girl...mriyagarg453
 
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser... Shivani Pandey
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingNitya salvi
 
(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With... Shivani Pandey
 
Verified Trusted Call Girls Egmore Chennai ✔✔7427069034 Independent Chennai ...
Verified Trusted Call Girls Egmore Chennai ✔✔7427069034  Independent Chennai ...Verified Trusted Call Girls Egmore Chennai ✔✔7427069034  Independent Chennai ...
Verified Trusted Call Girls Egmore Chennai ✔✔7427069034 Independent Chennai ... Shivani Pandey
 
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...SUHANI PANDEY
 
Mira Road | Call Girls Service Mumbai | ₹,9500 Pay Cash 9833325238 Free Home ...
Mira Road | Call Girls Service Mumbai | ₹,9500 Pay Cash 9833325238 Free Home ...Mira Road | Call Girls Service Mumbai | ₹,9500 Pay Cash 9833325238 Free Home ...
Mira Road | Call Girls Service Mumbai | ₹,9500 Pay Cash 9833325238 Free Home ...hotbabesbook
 
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...SUHANI PANDEY
 
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.Nitya salvi
 

Kürzlich hochgeladen (20)

VIP ( Goa Call Girls ) Margao Beach👉 8617370543 Escort Service Enjoy Your Dre...
VIP ( Goa Call Girls ) Margao Beach👉 8617370543 Escort Service Enjoy Your Dre...VIP ( Goa Call Girls ) Margao Beach👉 8617370543 Escort Service Enjoy Your Dre...
VIP ( Goa Call Girls ) Margao Beach👉 8617370543 Escort Service Enjoy Your Dre...
 
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
 
Dum Dum ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready...
Dum Dum ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready...Dum Dum ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready...
Dum Dum ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready...
 
📞 Contact Number 8617370543VIP Rajsamand Call Girls
📞 Contact Number 8617370543VIP Rajsamand Call Girls📞 Contact Number 8617370543VIP Rajsamand Call Girls
📞 Contact Number 8617370543VIP Rajsamand Call Girls
 
Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...
Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...
Jodhpur Park ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi ...
 
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRLBhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
Bhimtal ❤CALL GIRL 8617697112 ❤CALL GIRLS IN Bhimtal ESCORT SERVICE❤CALL GIRL
 
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034 Independe...
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034  Independe...Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034  Independe...
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034 Independe...
 
VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...
VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...
VIP Model Call Girls Vijayawada ( Pune ) Call ON 8005736733 Starting From 5K ...
 
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
 
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
 
WhatsApp Chat: 📞 8617697112 Hire Call Girls Raiganj For a Sensual Sex Experience
WhatsApp Chat: 📞 8617697112 Hire Call Girls Raiganj For a Sensual Sex ExperienceWhatsApp Chat: 📞 8617697112 Hire Call Girls Raiganj For a Sensual Sex Experience
WhatsApp Chat: 📞 8617697112 Hire Call Girls Raiganj For a Sensual Sex Experience
 
Navsari Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girl...
Navsari Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girl...Navsari Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girl...
Navsari Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girl...
 
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...
Model Call Girls In Pazhavanthangal WhatsApp Booking 7427069034 call girl ser...
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Nungambakkam Phone 7427069034 Call Girls Model With...
 
Verified Trusted Call Girls Egmore Chennai ✔✔7427069034 Independent Chennai ...
Verified Trusted Call Girls Egmore Chennai ✔✔7427069034  Independent Chennai ...Verified Trusted Call Girls Egmore Chennai ✔✔7427069034  Independent Chennai ...
Verified Trusted Call Girls Egmore Chennai ✔✔7427069034 Independent Chennai ...
 
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
VIP Model Call Girls Budhwar Peth ( Pune ) Call ON 8005736733 Starting From 5...
 
Mira Road | Call Girls Service Mumbai | ₹,9500 Pay Cash 9833325238 Free Home ...
Mira Road | Call Girls Service Mumbai | ₹,9500 Pay Cash 9833325238 Free Home ...Mira Road | Call Girls Service Mumbai | ₹,9500 Pay Cash 9833325238 Free Home ...
Mira Road | Call Girls Service Mumbai | ₹,9500 Pay Cash 9833325238 Free Home ...
 
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Koregaon Park ( Pune ) Call ON 8005736733 Starting From ...
 
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
❤Personal Whatsapp Number Keylong Call Girls 8617697112 💦✅.
 

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