Rahmadnet

Factory

Feb 26th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. 1. what is Factory patten
  2.     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.
  3.  
  4. 2. what are their benefit
  5.     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.
  6.  
  7. 3. what are their drawback
  8.     Kelemahan dari desing pattern ini adalah class akan semakin banyak.
  9.    
  10. 4. when to use it at best
  11.     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.
  12.  
  13. 5. when to NOT use it
  14.     Jika produk yang kita buat sudah cukup dan tidak ingin menambahkan produk baru dan objek baru. 
  15.    
  16.  
  17.  
  18. * Class Main
  19.  
  20. import java.util.Scanner;
  21.  
  22. public class EnemyTesting
  23. {
  24.     public static void main(String[] args)
  25.     {
  26.         Factory shipFactory = new Factory();
  27.        
  28.         EnemyShip theEnemy = null;
  29.        
  30.         Scanner input = new Scanner(System.in);
  31.        
  32.         System.out.print("What type ship? (U / R / B)");
  33.        
  34.         if(input.hasNextLine())
  35.         {
  36.             String typeOfShip = input.nextLine();
  37.            
  38.             theEnemy = shipFactory.makeEnemyShip(typeOfShip);
  39.         }
  40.        
  41.         if(theEnemy != null)
  42.         {
  43.             doStuffEnemy(theEnemy);
  44.         }
  45.         else System.out.println("Enter a U,R or B next time");
  46.        
  47.     }
  48.    
  49.     public static void doStuffEnemy(EnemyShip anEnemyShip)
  50.     {
  51.         anEnemyShip.displayEnemyShip();
  52.         anEnemyShip.followHeroShip();
  53.         anEnemyShip.enemyShipShots();
  54.     }
  55. }
  56.  
  57. * Class Utama (EnemyShip)
  58.  
  59. public abstract class EnemyShip
  60. {
  61.     private String name;
  62.     private double amtDamage;
  63.    
  64.     public String getName()
  65.     {
  66.         return name;
  67.     }
  68.    
  69.     public void setName(String newName)
  70.     {
  71.         name = newName;
  72.     }
  73.    
  74.     public double getDamage()
  75.     {
  76.         return amtDamage;
  77.     }
  78.    
  79.     public void setDamage(double newDamage)
  80.     {
  81.         amtDamage = newDamage;
  82.     }
  83.    
  84.     public void followHeroShip()
  85.     {
  86.         System.out.println(getName() + " is following the hero");
  87.     }
  88.    
  89.     public void displayEnemyShip()
  90.     {
  91.         System.out.println(getName()+ " is on the screen");
  92.     }
  93.    
  94.     public void enemyShipShots()
  95.     {
  96.         System.out.println(getName()+ " attacks and does " + getDamage());
  97.     }
  98. }
  99.  
  100.  
  101. * Sub Class (UFOEnemyShip)
  102.  
  103. public class UFOEnemyShip extends EnemyShip
  104. {
  105.     public UFOEnemyShip()
  106.     {
  107.         setName("UFO Enemy Ship");
  108.         setDamage(20.0);
  109.     }
  110. }
  111.  
  112. * Sub CLas (RocketEnemyShip)
  113.  
  114. public class RocketEnemyShip extends EnemyShip
  115. {
  116.     public RocketEnemyShip()
  117.     {
  118.         setName("Rocket Enemy Ship");
  119.         setDamage(10.0);
  120.     }
  121. }
  122.  
  123. * Sub Class (BigUFOEnemyShip)
  124.  
  125. public class BigUFOEnemyShip extends UFOEnemyShip
  126. {
  127.     public BigUFOEnemyShip()
  128.     {
  129.         setName("Big UFO Enemy Ship");
  130.         setDamage(40.0);
  131.     }
  132. }
  133.  
  134. * Class Factory
  135.  
  136. public class Factory
  137. {
  138.     public EnemyShip makeEnemyShip(String newShipType)
  139.     {
  140.         EnemyShip newShip = null;
  141.        
  142.         if(newShipType.equalsIgnoreCase("U"))
  143.         {
  144.             return new UFOEnemyShip();
  145.         }
  146.         else
  147.         {
  148.             if(newShipType.equalsIgnoreCase("R"))
  149.             {
  150.                 return new RocketEnemyShip();
  151.             }
  152.             else
  153.             {
  154.                 if(newShipType.equalsIgnoreCase("B"))
  155.                 {
  156.                     return new BigUFOEnemyShip();
  157.                 }
  158.                 else return null;
  159.             }
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment