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 Mampu menerapkan Simple Factory & Factory Method pattern untuk meningkatkan fleksibilitas kode
Koleksi Desain Pattern Creational Patterns : Singleton Factory
Persoalan
Example
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
SIMPLE FACTORY
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;
public class PizzaStore { public Pizza orderPizza(String type){ Pizza pizza; pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } //Keluarkan Kode & Buat objek khusus untuk // membuat Pizza PizzaFactory
public class SimplePizzaFactory { Buat kelas factory untuk menghasilkan objek 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;
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
Class Diagram : Simple Factory Pizza prepare() bake() cut() box() PizzaStore orderPizza() SimplePizzaFactory createPizza() ClamPizza CheesePizza PepperoniPizza GreekPizza
FACTORY METHOD
NYPizzaStore nyFactory = new NYPizzaFactory(); ChicagoPizzaStore PizzaStore NYPizzaStore NYPizzaStore nyFactory = new NYPizzaFactory(); PizzaStore nyStore = new PizzaStore(nyFactory); nyStore.order(“cheese”); ChicacoPizzaStore chicagoFactory = new ChicacoPizzaFactory(); PizzaStore chicagoStore = new PizzaStore (chicagoFactory); chicagoStore.order(“cheese”);
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 Prinsip : Program to an interface not an implementation
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;
Factory Method
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 NYGreekPizza extends Pizza { public NYGreekPizza () { name = "NY Style Greek Pizza"; dough = "Thin Crust dough"; sauce = "Barbeque Sauce"; topping = "Mushroom with Parmesan"; } 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 ");
public class TestPizza { public static void main(String[] args) { PizzaStore nyStore = 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
All factory patterns encapsulate object creation. The Factory Method Pattern encapsulates object creation by letting subclasses decide what objects to create.
ChicagoPepperoniPizza The Creator Classes The Product Classes Pizza PizzaStore NYCheesePizza ChicagoCheesePizza ChicagoPizzaStore NYPepperoniPizza ChicagoPepperoniPizza NYGreekPizza ChicagoGreekPizza NYPizzaStore
Factory Method -- Definisi The Factory Method Pattern defines an interface for creating an object, but lets subclass decide which class to instantiate
Factory Method – Class Diagram
Referensi http://www.eelke.com/files/cs330/factory.pdf Head First Design Pattern
Latihan Terapkan Simple Factory pada contoh DuckStore. 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 bebek dengan warna "+color); public void pack(){ System.out.println("Masukkan mainan bebek " +material+" ke dalam kotak"); 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"); }
DuckStore DuckStore(SimpleDuckFactory f) orderDuck(String type) SimpleDuckFactory factory Duck Duck(Stringcolor, String material) paint() pack() String color String material SimpleDuckFactory createDuck(String type) RubberDuck RubberDuck() DecoyDuck DecoyDuck()
Terapkan Factory Method utk skenario di atas. Ada 2 pabrik pembuat mainan bebek yaitu FactoryA dan FactoryB dimana masing-masing membuat kedua jenis bebek (Rubber dan Decoy). Penjelasan : Terapkan Factory Method utk skenario di atas. Tipe Color Material RubberDuckA ORANGE STANDAR PLASTIC DecoyDuckA BLACK MERANTI WOOD RubberDuckB YELLOW PREMIUM PLASTIC DecoyDuckB BROWN JATI WOOD
Duck Duck(Stringcolor, String material) paint() pack() String color String material DuckFactory createDuck(String type) FactoryA FactoryB RubberDuckA RubberDuck(color, material) DecoyDuckB DecoyDuck(color, material) DecoyDuckA DecoyDuck(color, material) RubberDuckB RubberDuck(color, material)
ABSTRACT FACTORY
Contoh Kasus Pizza Store Tiap pizza komposisinya : dough, sauce, cheese, veggies, dan clams. Tiap lokasi bisa berbeda jenis komposisi. Komposisi New York Chicago Dough thin & crust Thick & crust Sauce Marinara Plum tomato Cheese Reggiano Mozzarella Clams Fresh Frozen
Bagaimana memastikan bahwa Pizza Store di NY, Chicago, dll akan menggunakan komposisi bahan yg sama? Bagaimana membuat sebuah kelas yang dapat men-create serangkaian produk?
Solusi Buat abstrak/interface sbg factory untuk create serangkaian produk, contoh : PizzaIngredientsFactory Buat kelas : Dough, Sauce, Cheese, Clams beserta subclass masing-masing.
Solusi (cont.) Buat subclass dari factory, dlm hal ini PizzaIngredientsFactory
Buat kelas Pizza
Buat subclass dari Pizza, misalnya CheesePizza, ClamPizza, dll
Buat kelas PizzaStore dan turunannya : NYPizzaStore, ChicagoPizzaStore, dll.
Contoh main class
Abstract Factory
Definisi The Abstract Factory Pattern provides an interface for creating a related or dependent objects without specifying their concrete classes
Aplikasi Klien terpisah dari bagaimana produk terbentuk. Objek dibuat sebagai satu kesatuan Menyediakan sekumpulan kelas namun tidak menyediakan implementasinya.
Terapkan Abstract Factory! Contoh 2 Aplikasi untuk mendesain taman. Sebuah taman diisi 3 jenis tumbuhan yang lokasi berbeda yaitu sisi pusat, tepi, dan pojok. Tanaman untuk ketiga lokasi ini berbeda tergantung tema tamannya : tahunan (annual), sayuran (vegetables), atau sepanjang tahun (perennial). Tema Pusat Tepi Pojok Annual Dicentrum Sedum Astilbe Vegetables Brocolli Corn Peas Perennial Anemone Dianthus Aster Terapkan Abstract Factory!
Solusi Buat abstract factory : GardenFactory Buat kelas : Tanaman Buat subkelas Tanaman : broccoli, peas, corn, astilbe, dicentrum, sedum Buat class : Garden Buat subkelas GardenFactory : VegieGarden, AnnualGarden, dan PerennialGarden
Tugas Buat juga untuk AnnualGarden dan PerennialGarden Buat program pengujinya.