0
Factory Pattern
Factory Pattern
Duck duck;
if (picnic) {
duck = new MallardDuck();
} else if (hunting) {
duck = new DecoyDuck();
} else if...
Factory Pattern(Contd.)
Pizza orderPizza(String type) {
Pizza pizza;
if (type.equals(“cheese”)) {
pizza = new CheesePizza(...
Factory Pattern (Contd.)
public class SimplePizzaFactory {
public Pizza createPizza(String type) {
Pizza pizza = null;
if ...
Factory Pattern (Contd.)
public class PizzaStore {
SimplePizzaFactory factory;
public PizzaStore(SimplePizzaFactory
factor...
Factory Pattern(Contd.)
NYPizzaFactory nyFactory = new NYPizzaFactory();
PizzaStore nyStore = new PizzaStore(nyFactory);
n...
Factory Pattern(Contd.)
public abstract class PizzaStore {
public Pizza orderPizza(String type) {
Pizza pizza;
pizza = cre...
Factory Pattern(Contd.)
public class NYStylePizzaStore extends PizzaStore {
public Pizza createPizza(type) {
if (type.equa...
Factory Pattern(Contd.)
public class ChicagoStylePizzaStore extends PizzaStore {
public Pizza createPizza(type) {
if (type...
Factory Pattern (Contd.)
public abstract class Pizza {
String name;
String dough;
String sauce;
ArrayList<String> toppings...
Factory Pattern(Contd.)
public class NYStyleCheesePizza extends
Pizza {
public NYStyleCheesePizza() {
name = “NY Style SCh...
Factory Pattern(Contd.)
public class PizzaTestDrive {
public static void main(String[] args) {
PizzaStore nyStore = new NY...
Factory Pattern(Contd.)
● The Factory Method Pattern defines an interface for
creating an object, but lets subclasses decid...
Dependency Inversion Principle
● Depend upon abstractions. Do not depend upon
concrete classes.
● A successor of this prin...
Dependency Inversion Guidelines
● No variable should hold a reference to a concrete
class.
● No class should derive from a...
Resources
● Head First Design Pattern
Thank You
Upcoming SlideShare
Loading in...5
×

Factory Method Pattern

409

Published on

I used this slide to conduct a training session on Factory Method pattern in my company (IMpulse). The examples were taken from the famous Head First Design Pattern book.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
409
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Factory Method Pattern"

  1. 1. Factory Pattern
  2. 2. Factory Pattern Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new DecoyDuck(); } else if (inBathTub) { duck = new RubberDuck(); }
  3. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 16. Resources ● Head First Design Pattern
  17. 17. Thank You
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×