290 likes | 587 Views
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
E N D
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
KoleksiDesain Pattern • Creational Patterns : • Singleton • Factory
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
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; } } //KeluarkanKode & Buatobjekkhususuntuk // membuat Pizza PizzaFactory
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; } }
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
Class Diagram : Simple Factory SimplePizzaFactory Pizza PizzaStore ClamPizza GreekPizza PepperoniPizza CheesePizza createPizza() orderPizza() prepare() bake() cut() box()
PizzaStore ChicagoPizzaStore NYPizzaStore NYPizzaStorenyFactory = new NYPizzaFactory(); PizzaStorenyStore = new PizzaStore(nyFactory); nyStore.order(“cheese”); ChicacoPizzaStorechicagoFactory = new ChicacoPizzaFactory(); PizzaStorechicagoStore = 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
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; } }
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 "); • } • }
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
All factory patterns encapsulate object creation. The Factory Method Pattern encapsulates object creation by letting subclasses decide what objects to create.
The Creator Classes The Product Classes PizzaStore ChicagoPizzaStore NYPizzaStore Pizza NYCheesePizza NYPepperoniPizza NYGreekPizza ChicagoCheesePizza ChicagoPepperoniPizza ChicagoGreekPizza
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.
Referensi http://www.eelke.com/files/cs330/factory.pdf Head First Design Pattern
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.
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()
Ada 2 pabrikpembuatmainanbebekyaituFactoryAdanFactoryBdimanamasing-masingmembuatkeduajenisbebek (Rubber dan Decoy). Penjelasan : TerapkanFactory Methodutkskenariodiatas.
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