The Factory Pattern.

Slides:



Advertisements
Presentasi serupa
Pemrograman Berorientasi Objek
Advertisements

Other OOP Basic Viska Mutiawani, M.Sc. Konsep penting Method overloading Encapsulation this keyword final static.
ABSTRACT CLASS Imam Fahrur Rozi.
Workshop SCS: Java Game Programming
Singleton Pattern 1 one uno. What is Singleton ? One object of a kind Ada class yang hanya perlu diinstansiasi 1 kali saja Alasan: Dalam beberapa kasus.
Lecture 2 Introduction to C# - Object Oriented Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
POLIMORFISME 2 Imam Fahrur Rozi 01.
Pemrograman Berbasis Obyek
Pemrograman Berorientasi Obyek Oleh Tita Karlita
KELAS INNER, KELAS ABSTRACT, DAN INTERFACE
Abstract Class.
Pertemuan : Object Oriented Programming
OBJECT ORIENTED PROGRAMMING YANES HARDIANTO SUI Politeknik Manufaktur Astra 2011.
Flow Control & Looping Pertemuan 4 Pemrograman Berbasis Obyek Oleh Tita Karlita.
Class and Object Introduction Specifying a Class Defining Member Function A C++ Program with Class Nesting of Member Functions Private Member Functions.
OOP Java 06 Polymorphism.
Kelas Lanjut 2 Oleh Tita Karlita.
INHERITANCE (Oleh : Nur Hayatin, S.ST)
OOP Java Minggu 2b Dasar OOP. Class (1) Deklarasi class : [ *] class { [ *] } Cat : [] = optional, * = repeat 0-N.
Abstract & Interface GOF Design Pattern
Pertemuan 6 PEWARISAN AND POLYMORPHISM
Java Generics.
The Strategy Pattern.
PERTEMUAN KE-6 UNIFIED MODELLING LANGUAGE (UML) (Part 2)
Bayu Priyambadha, S.Kom.  Classes, which are the "blueprints" for an object and are the actual code that defines the properties and methods.  Objects,
Introduction to object
Access Modifier.
PBO Daniel Riano Kaparang, S.Kom., M.Cs
POLYMORPHISM (KEBANYAKRUPAAN)
Pertemuan 2 SINTAKS BAHASA, TIPE DATA, DAN OPERATOR
Pemrograman Berorientasi Object
Constructor dan Overloading
POLYMORPHISM (KEBANYAKRUPAAN)
POLYMORPHISM (Overloading)
Pengorganisasian Class
Fondasi Pemrograman & Struktur Data
MODIFIER JAVA.
Algoritma & Pemrograman 1
Pengorganisasian Class
INTERFACE Pertemuan 005.
ABSTRACT CLASS DAN INTERFACE
FACTORY METHOD PATTERN
Association, Composition dan Inheritance
THREAD Pertemuan 9.
KELAS DAN OBJEK BAGIAN I Pertemuan 6.
Skenario 2: Hujan Pemrograman Game Eko Prasetyo Teknik Informatika
Linear Data Structures (Array)
Tipe Data, Variabel, dan Operator
Interface.
Matakuliah : M0864/Programming I
Tipe Data, Variabel, dan Operator
Membuat Kelas.
MATERI PENDUKUNG PENGENALAN DASAR CLASS
Karakteristik Pemrograman Berorientasi Objek
BPJ – Pertemuan 13 OOP.
PBO Lanjutan Membuat Kelas.
ABSTRACT CLASS DAN INTERFACE
Pemrograman Berorientasi Object
STUDI KASUS.
Interface pada Java Untuk memahami lebih mudah, interface sekumpulan dari method-method yang dibuat tapi belum ada operasi di dalam tubuh method tersebut,
Pilar Object Oriented Programming
Pemrograman Berorientasi Objek <PBO>
OOP ENKAPSULASI SMKN 2 SINGOSARI Kelas XI RPL.
E. Haodudin Nurkifli Universitas Ahmad Dahlan Pertemuan
Tipe Data, Variabel, dan Operator
Pertemuan 14 Class Diagram.
Pemrograman berorientasi objek
Pemrograman Berorientasi Objek
FONDASI PEMROGRAMAN & STRUKTUR DATA #6
Bahasa Pemrograman (Pemrograman Visual)
Transcript presentasi:

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.