RSS

Factory Method


The factory method pattern is an object-oriented design pattern to implement the concept of factories.

Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The factory method design pattern handles this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.

Outside the scope of design patterns, the term factory method can also refer to a method of a factory whose main purpose is creation of objects.


Contoh Kasus :

- Mengecat Pintu Mobil dengan pola yang berbeda, pintu ada 4, jenis mobil terserah.


1. create class door.java

public interface door {

public Car draw();

}


2. create class Car.java

public interface Car {

public void drawModel();

public void drawPola();

}


3. create class getDraw.java
public class getDraw {
private Car drawer;
public getDraw (Car draw){
drawer=draw;
}

public void cetak (){
drawer.drawModel();
drawer.drawPola();
}
}

4. create class jeep.java
public class jeep implements door {
public Car draw (){
return new drawing();
}

private class drawing implements Car {
public void drawModel() {
System.out.println("Pintu Mobil Jeep");
}
public void drawPola(){
System.out.println("Cat Pola Circle");
}

}
}

4. create class sedan.java
5. create class vw_kodok.java
6. create main.java

Untuk selengkapnya program bisa di download di Contoh Program

Semoga bermanfaat yaa...good luck...^_^

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS
Read Comments