the factory pattern n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
The Factory Pattern PowerPoint Presentation
Download Presentation
The Factory Pattern

Loading in 2 Seconds...

play fullscreen
1 / 29

The Factory Pattern - PowerPoint PPT Presentation


  • 258 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'The Factory Pattern' - desiree-burgess


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide2

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

slide3

KoleksiDesain Pattern

  • Creational Patterns :
    • Singleton
    • Factory
slide7

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
slide9

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;

}

}

slide10

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

slide11

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;

}

}

slide12

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

slide13

Class Diagram : Simple Factory

SimplePizzaFactory

Pizza

PizzaStore

ClamPizza

GreekPizza

PepperoniPizza

CheesePizza

createPizza()

orderPizza()

prepare()

bake()

cut()

box()

slide15

PizzaStore

ChicagoPizzaStore

NYPizzaStore

NYPizzaStorenyFactory = new NYPizzaFactory();

PizzaStorenyStore = new PizzaStore(nyFactory);

nyStore.order(“cheese”);

ChicacoPizzaStorechicagoFactory = new ChicacoPizzaFactory();

PizzaStorechicagoStore = new PizzaStore (chicagoFactory);

chicagoStore.order(“cheese”);

slide16

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

slide17

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;

}

}

slide19

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

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

slide21

All factory patterns encapsulate object creation.

The Factory Method Pattern encapsulates object creation by letting subclasses decide what objects to create.

slide22

The Creator Classes

The Product Classes

PizzaStore

ChicagoPizzaStore

NYPizzaStore

Pizza

NYCheesePizza

NYPepperoniPizza

NYGreekPizza

ChicagoCheesePizza

ChicagoPepperoniPizza

ChicagoGreekPizza

slide23

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.

slide25

Referensi

http://www.eelke.com/files/cs330/factory.pdf

Head First Design Pattern

latihan
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.

slide27

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

slide28

Ada 2 pabrikpembuatmainanbebekyaituFactoryAdanFactoryBdimanamasing-masingmembuatkeduajenisbebek (Rubber dan Decoy). Penjelasan :

TerapkanFactory Methodutkskenariodiatas.

slide29

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