1 / 44

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

catrin
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 { 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; } } 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 subclass decide which class to instantiate

  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

  29. ABSTRACT FACTORY

  30. ContohKasus Pizza Store Tiap pizza komposisinya : dough, sauce, cheese, veggies, dan clams. Tiaplokasibisaberbedajeniskomposisi.

  31. Bagaimanamemastikanbahwa Pizza Store di NY, Chicago, dllakanmenggunakankomposisibahanygsama? Bagaimanamembuatsebuahkelas yang dapat men-create serangkaianproduk?

  32. Solusi Buatabstrak/interface sbg factory untuk create serangkaianproduk, contoh : PizzaIngredientsFactory Buatkelas : Dough, Sauce, Cheese, Clams beserta subclass masing-masing.

  33. Solusi (cont.) Buat subclass dari factory, dlmhaliniPizzaIngredientsFactory

  34. Buatkelas Pizza

  35. Buat subclass dari Pizza, misalnyaCheesePizza, ClamPizza, dll

  36. BuatkelasPizzaStoredanturunannya : NYPizzaStore, ChicagoPizzaStore, dll.

  37. Contoh main class

  38. Abstract Factory

  39. Definisi The Abstract Factory Pattern provides an interface for creating a related or dependent objects without specifying their concrete classes

  40. Aplikasi Klienterpisahdaribagaimanaprodukterbentuk. Objekdibuatsebagaisatukesatuan Menyediakansekumpulankelasnamuntidakmenyediakanimplementasinya.

  41. Contoh 2 Terapkan Abstract Factory! Aplikasiuntukmendesaintaman. Sebuahtamandiisi 3 jenistumbuhan yang lokasiberbedayaitusisipusat, tepi, danpojok. Tanamanuntukketigalokasiiniberbedatergantungtematamannya : tahunan (annual), sayuran (vegetables), atausepanjangtahun (perennial).

  42. Solusi Buat abstract factory : GardenFactory Buatkelas : Tanaman BuatsubkelasTanaman : broccoli, peas, corn, astilbe, dicentrum, sedum Buat class : Garden BuatsubkelasGardenFactory : VegieGarden, AnnualGarden, danPerennialGarden

  43. Tugas BuatjugauntukAnnualGardendanPerennialGarden Buat program pengujinya.

More Related