1 / 29

The Factory Pattern

The Factory Pattern. Tujuan Pembelajaran. Mengetahui model persoalan yang menggunakan Simple Factory & Factory Method pattern Mengetahui bagaimana menerapkan Simple Factory & Factory Method pattern pada program

Download Presentation

The Factory Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Factory Pattern

  2. TujuanPembelajaran Mengetahui model persoalan yang menggunakan Simple Factory & Factory Method pattern Mengetahuibagaimanamenerapkan Simple Factory & Factory Method pattern pada program Mampumenerapkan Simple Factory & Factory Method pattern untukmeningkatkanfleksibilitaskode

  3. KoleksiDesain Pattern • Creational Patterns : • Singleton • Factory

  4. Persoalan

  5. Example

  6. Program to an interface not an implementation • You should be open for extension, yet closed for modification • Identify the aspects that vary and separate them from what stays the same

  7. SIMPLE FACTORY

  8. public class PizzaStore { public Pizza orderPizza(String type){ Pizza pizza; if (type.equals("cheese")){ pizza = new CheesePizza(); }else if (type.equals("pepperoni")){ pizza = new PepperoniPizza(); }else if (type.equals("greek")){ pizza = new GreekPizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } }

  9. public class PizzaStore { public Pizza orderPizza(String type){ Pizza pizza; pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } } //KeluarkanKode & Buatobjekkhususuntuk // membuat Pizza  PizzaFactory

  10. Buatkelas factory untukmenghasilkanobjek Pizza 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("greek")){ pizza = new GreekPizza(); } return pizza; } }

  11. public class PizzaStore { SimplePizzaFactoryfactory; 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; } } composition

  12. Class Diagram : Simple Factory SimplePizzaFactory Pizza PizzaStore ClamPizza GreekPizza PepperoniPizza CheesePizza createPizza() orderPizza() prepare() bake() cut() box()

  13. FACTORY METHOD

  14. PizzaStore ChicagoPizzaStore NYPizzaStore NYPizzaStorenyFactory = new NYPizzaFactory(); PizzaStorenyStore = new PizzaStore(nyFactory); nyStore.order(“cheese”); ChicacoPizzaStorechicagoFactory = new ChicacoPizzaFactory(); PizzaStorechicagoStore = new PizzaStore (chicagoFactory); chicagoStore.order(“cheese”);

  15. public abstract class PizzaStore { • public Pizza orderPizza(String type){ • Pizza pizza; • pizza = createPizza(type); • pizza.prepare(); • pizza.bake(); • pizza.cut(); • pizza.box(); • return pizza; • } • public abstract Pizza createPizza(String type); • } PizzaStore ChicagoPizzaStore NYPizzaStore

  16. ChicagoPizzaStore Class NYPizzaStore Class public class NYPizzaStore extends PizzaStore { public Pizza createPizza(String type) { Pizza pizza = null; if (type.equals("cheese")){ pizza = new NYCheesePizza(); } else if (type.equals("pepperoni")){ pizza = new NYPepperoniPizza(); }else if (type.equals("greek")){ pizza = new NYGreekPizza(); } return pizza; } } public class ChicagoPizzaStore extends PizzaStore { public Pizza createPizza(String type) { Pizza pizza = null; if (type.equals("cheese")){ pizza = new ChicagoCheesePizza(); } else if (type.equals("pepperoni")){ pizza = new ChicagoPepperoniPizza(); } else if (type.equals("greek")){ pizza = new ChicagoGreekPizza(); } return pizza; } }

  17. Factory Method

  18. public class NYGreekPizza extends Pizza { • public NYGreekPizza () { • name = "NY Style Greek Pizza"; • dough = "Thin Crust dough"; • sauce = "Barbeque Sauce"; • topping = "Mushroom with Parmesan"; • } • } • public class NYCheesePizza extends Pizza { • public NYCheesePizza(){ • name = "NY Style Sauce and Cheese Pizza"; • dough = "Thin Crust dough"; • sauce = "Marinara Sauce"; • topping = "Grated Reggiano Cheese"; • } • } • public class NYPepperoniPizza extends Pizza { • public NYPepperoniPizza(){ • name = "Chicago Style Deep Dish Cheese Pizza"; • dough = "Extra Thick Crust Dough"; • sauce = "Plum Tomato Sauce"; • topping = "Shredded Mozzarella"; • } • public void cut (){ • System.out.println("Cutting the pizza into square slices "); • } • }

  19. public class TestPizza { • public static void main(String[] args) { • PizzaStorenyStore = new NYPizzaStore(); • Pizza pizza = nyStore.orderPizza("cheese"); • } • } Output : Preparing NY Style Sauce and Cheese Pizza Tossing dough ... Adding sauce ... Adding topping ... Grated Reggiano Cheese Bake for 25 minutes at 350 Cutting the pizza into diagonal slices Place pizza in official PizzaStore box

  20. All factory patterns encapsulate object creation. The Factory Method Pattern encapsulates object creation by letting subclasses decide what objects to create.

  21. The Creator Classes The Product Classes PizzaStore ChicagoPizzaStore NYPizzaStore Pizza NYCheesePizza NYPepperoniPizza NYGreekPizza ChicagoCheesePizza ChicagoPepperoniPizza ChicagoGreekPizza

  22. Factory Method -- Definisi 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.

  23. Factory Method – Class Diagram

  24. Referensi http://www.eelke.com/files/cs330/factory.pdf Head First Design Pattern

  25. Latihan public class Duck { String color; String material; public Duck(String color, String material) { this.color = color; this.material = material; } public void paint(){ System.out.println("Cat bebekdenganwarna "+color); } public void pack(){ System.out.println("Masukkanmainanbebek " +material+" kedalamkotak"); } } public class DuckStore { public void orderDuck(String type){ Duck duck = null; if (type.equals("rubber")){ duck = new RubberDuck(); } else { duck = new DecoyDuck(); } duck.paint(); duck.pack(); } } public class Test { public static void main(String [] args){ DuckStore store = new DuckStore(); System.out.println("Order RUBBER DUCK"); store.orderDuck("rubber"); System.out.println("Order DECOY DUCK"); store.orderDuck("decoy"); } } public class RubberDuck extends Duck { public RubberDuck() { super("YELLOW", "PLASTIC"); } } public class DecoyDuck extends Duck{ public DecoyDuck() { super("BROWN", "WOOD"); } } Terapkan Simple Factory padacontohDuckStore.

  26. Duck SimpleDuckFactory DecoyDuck DuckStore RubberDuck String color String material createDuck(String type) DecoyDuck() RubberDuck() DuckStore(SimpleDuckFactory f) orderDuck(String type) SimpleDuckFactory factory Duck(Stringcolor, String material) paint() pack()

  27. Ada 2 pabrikpembuatmainanbebekyaituFactoryAdanFactoryBdimanamasing-masingmembuatkeduajenisbebek (Rubber dan Decoy). Penjelasan : TerapkanFactory Methodutkskenariodiatas.

  28. Duck DuckFactory DecoyDuckA DecoyDuckB RubberDuckA RubberDuckB String color String material createDuck(String type) DecoyDuck(color, material) RubberDuck(color, material) DecoyDuck(color, material) RubberDuck(color, material) Duck(Stringcolor, String material) paint() pack() FactoryB FactoryA

More Related