Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

The Factory Pattern.

Presentasi serupa


Presentasi berjudul: "The Factory Pattern."— Transcript presentasi:

1 The Factory Pattern

2 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

3 Koleksi Desain Pattern
Creational Patterns : Singleton Factory

4 Persoalan

5 Example

6

7 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

8 SIMPLE FACTORY

9 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;

10 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

11 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;

12 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

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

14 FACTORY METHOD

15 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”);

16 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

17 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;

18 Factory Method

19 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 ");

20 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

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

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

23 Factory Method -- Definisi
The Factory Method Pattern defines an interface for creating an object, but lets subclass decide which class to instantiate

24 Factory Method – Class Diagram

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

26 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"); }

27 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()

28 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

29 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)

30 ABSTRACT FACTORY

31 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

32 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?

33 Solusi Buat abstrak/interface sbg factory untuk create serangkaian produk, contoh : PizzaIngredientsFactory Buat kelas : Dough, Sauce, Cheese, Clams beserta subclass masing-masing.

34 Solusi (cont.) Buat subclass dari factory, dlm hal ini PizzaIngredientsFactory

35 Buat kelas Pizza

36 Buat subclass dari Pizza, misalnya CheesePizza, ClamPizza, dll

37 Buat kelas PizzaStore dan turunannya : NYPizzaStore, ChicagoPizzaStore, dll.

38 Contoh main class

39 Abstract Factory

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

41 Aplikasi Klien terpisah dari bagaimana produk terbentuk.
Objek dibuat sebagai satu kesatuan Menyediakan sekumpulan kelas namun tidak menyediakan implementasinya.

42 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!

43 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

44 Tugas Buat juga untuk AnnualGarden dan PerennialGarden
Buat program pengujinya.


Download ppt "The Factory Pattern."

Presentasi serupa


Iklan oleh Google