Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. what is Factory patten
- Factory adalah contoh konsep yang dapat memisahkan pembuatan pada objek produk dengan dengan objek client(main class).secara konsep penggunaan patern ini adalah perubahan pada class produk tidak akan berpengaruh pada class client.
- 2. what are their benefit
- Manfaat desing pattern factory adalah sub class bisa menjadi super class umum sehingga method dari sub class tersebut bisa di waris kan ke class yang lain.
- 3. what are their drawback
- Kelemahan dari desing pattern ini adalah class akan semakin banyak.
- 4. when to use it at best
- Jika kita ingin mengetahui objek yang akan kita butuh kan sehinhgga kita tidak perlu menambahkan atau membuat objek baru pada class yang baru kita buat.
- 5. when to NOT use it
- Jika produk yang kita buat sudah cukup dan tidak ingin menambahkan produk baru dan objek baru.
- * Class Main
- import java.util.Scanner;
- public class EnemyTesting
- {
- public static void main(String[] args)
- {
- Factory shipFactory = new Factory();
- EnemyShip theEnemy = null;
- Scanner input = new Scanner(System.in);
- System.out.print("What type ship? (U / R / B)");
- if(input.hasNextLine())
- {
- String typeOfShip = input.nextLine();
- theEnemy = shipFactory.makeEnemyShip(typeOfShip);
- }
- if(theEnemy != null)
- {
- doStuffEnemy(theEnemy);
- }
- else System.out.println("Enter a U,R or B next time");
- }
- public static void doStuffEnemy(EnemyShip anEnemyShip)
- {
- anEnemyShip.displayEnemyShip();
- anEnemyShip.followHeroShip();
- anEnemyShip.enemyShipShots();
- }
- }
- * Class Utama (EnemyShip)
- public abstract class EnemyShip
- {
- private String name;
- private double amtDamage;
- public String getName()
- {
- return name;
- }
- public void setName(String newName)
- {
- name = newName;
- }
- public double getDamage()
- {
- return amtDamage;
- }
- public void setDamage(double newDamage)
- {
- amtDamage = newDamage;
- }
- public void followHeroShip()
- {
- System.out.println(getName() + " is following the hero");
- }
- public void displayEnemyShip()
- {
- System.out.println(getName()+ " is on the screen");
- }
- public void enemyShipShots()
- {
- System.out.println(getName()+ " attacks and does " + getDamage());
- }
- }
- * Sub Class (UFOEnemyShip)
- public class UFOEnemyShip extends EnemyShip
- {
- public UFOEnemyShip()
- {
- setName("UFO Enemy Ship");
- setDamage(20.0);
- }
- }
- * Sub CLas (RocketEnemyShip)
- public class RocketEnemyShip extends EnemyShip
- {
- public RocketEnemyShip()
- {
- setName("Rocket Enemy Ship");
- setDamage(10.0);
- }
- }
- * Sub Class (BigUFOEnemyShip)
- public class BigUFOEnemyShip extends UFOEnemyShip
- {
- public BigUFOEnemyShip()
- {
- setName("Big UFO Enemy Ship");
- setDamage(40.0);
- }
- }
- * Class Factory
- public class Factory
- {
- public EnemyShip makeEnemyShip(String newShipType)
- {
- EnemyShip newShip = null;
- if(newShipType.equalsIgnoreCase("U"))
- {
- return new UFOEnemyShip();
- }
- else
- {
- if(newShipType.equalsIgnoreCase("R"))
- {
- return new RocketEnemyShip();
- }
- else
- {
- if(newShipType.equalsIgnoreCase("B"))
- {
- return new BigUFOEnemyShip();
- }
- else return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment