Advertisement
rafibatam

Design Pattern Builder JAVA

Mar 23rd, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.70 KB | None | 0 0
  1. Builder Pattern adalah salah satu contoh konsep design pattern yang dapat digunakan untuk membangun object kompleks yang memiliki banyak atribut. Contoh ketika ingin menampilkan semua nilai dalam satu object yang tidak bisa dilakukan oleh pattern lain seperti factory pattern dan abstract factory karena keterbatasan penggunanya, kita bisa menggunakan pattern ini sebagai cara alternatif.
  2.  
  3. // Builder.java
  4. public interface Builder {
  5.     void setCamera(int Camera);
  6.     void setType(String Type);
  7.     void setMemory(int RAM);
  8.     void setInternal(int Internal);
  9.     void setLayar(String Layar);
  10.     void setCPU(String CPU);
  11.     void setBattery(int Battery);
  12. }
  13.  
  14. // PhoneBuilder.java
  15. public class PhoneBuilder implements Builder {
  16.     private int Camera;
  17.     private String Type;
  18.     private int RAM;
  19.     private int Internal;
  20.     private String Layar;
  21.     private String CPU;
  22.     private int Battery;
  23.  
  24.     public void setMemory(int RAM) {
  25.         this.RAM = RAM;
  26.     }
  27.  
  28.     public void setInternal(int Internal) {
  29.         this.Internal = Internal;
  30.     }
  31.  
  32.     public void setLayar(String Layar) {
  33.         this.Layar = Layar;
  34.     }
  35.  
  36.     public void setCPU(String CPU) {
  37.         this.CPU = CPU;
  38.     }
  39.  
  40.     public void setBattery(int Battery) {
  41.         this.Battery = Battery;
  42.     }
  43.    
  44.     public Phone getResult() {
  45.         return new Phone(Type, Camera, RAM, Internal, Layar, CPU, Battery);
  46.     }
  47.  
  48.     public void setType(String Type) {
  49.         this.Type = Type;
  50.     }
  51.  
  52.     public void setCamera(int Camera) {
  53.         this.Camera = Camera;
  54.     }
  55. }
  56.  
  57. // PhoneManualBuilder.java
  58. public class PhoneManualBuilder implements Builder {
  59.     private int Camera;
  60.     private String Type;
  61.     private int RAM;
  62.     private int Internal;
  63.     private String Layar;
  64.     private String CPU;
  65.     private int Battery;
  66.  
  67.     public void setMemory(int RAM) {
  68.         this.RAM = RAM;
  69.     }
  70.  
  71.     public void setInternal(int Internal) {
  72.         this.Internal = Internal;
  73.     }
  74.  
  75.     public void setLayar(String Layar) {
  76.         this.Layar = Layar;
  77.     }
  78.  
  79.     public void setCPU(String CPU) {
  80.         this.CPU = CPU;
  81.     }
  82.  
  83.     public void setBattery(int Battery) {
  84.         this.Battery = Battery;
  85.     }
  86.    
  87.     public Manual getResult() {
  88.         return new Manual(Type, Camera, RAM, Internal, Layar, CPU, Battery);
  89.     }
  90.  
  91.     public void setType(String Type) {
  92.         this.Type = Type;
  93.     }
  94.  
  95.     public void setCamera(int Camera) {
  96.         this.Camera = Camera;
  97.     }
  98. }
  99.  
  100. // Phone.java
  101. public class Phone {
  102.     private final int Camera;
  103.     private final String Type;
  104.     private final int RAM;
  105.     private final int Internal;
  106.     private final String Layar;
  107.     private final String CPU;
  108.     private final int Battery;
  109.     private int Lowbatt = 0;
  110.    
  111.     public Phone(String Type, int Camera, int RAM, int Internal, String Layar, String CPU, int Battery) {
  112.         this.Type = Type;
  113.         this.Camera = Camera;
  114.         this.RAM = RAM;
  115.         this.Internal = Internal;
  116.         this.Layar = Layar;
  117.         this.CPU = CPU;
  118.         this.Battery = Battery;
  119.     }
  120.    
  121.     public String setType() {
  122.         return Type;
  123.     }
  124.    
  125.     public int setCamera() {
  126.         return Camera;
  127.     }
  128.    
  129.     public int getRAM() {
  130.         return RAM;
  131.     }
  132.    
  133.     public int getInternal() {
  134.         return Internal;
  135.     }
  136.    
  137.     public String getLayar() {
  138.         return Layar;
  139.     }
  140.    
  141.     public String getCPU() {
  142.         return CPU;
  143.     }
  144.    
  145.     public int getBattery() {
  146.         return Battery;
  147.     }
  148.    
  149.     public String getType() {
  150.         return Type;
  151.     }
  152.    
  153.     public int getLowbatt() {
  154.         return Lowbatt;
  155.     }
  156.    
  157.     public void setLowbatt(int Lowbatt) {
  158.         this.Lowbatt = Lowbatt;
  159.     }
  160. }
  161.  
  162. // Manual.java
  163. public class Manual {
  164.     private final int Camera;
  165.     private final String Type;
  166.     private final int RAM;
  167.     private final int Internal;
  168.     private final String Layar;
  169.     private final String CPU;
  170.     private final int Battery;
  171.    
  172.     public Manual(String Type, int Camera, int RAM, int Internal, String Layar, String CPU, int Battery) {
  173.         this.Type = Type;
  174.         this.Camera = Camera;
  175.         this.RAM = RAM;
  176.         this.Internal = Internal;
  177.         this.Layar = Layar;
  178.         this.CPU = CPU;
  179.         this.Battery = Battery;
  180.     }
  181.    
  182.     public String print() {
  183.         String info = "";
  184.         info += "Type of Phone\t= " + Type;
  185.         info += "\nCPU\t\t= " + CPU;
  186.         info += "\nCamera\t\t= " + Camera + "MP";
  187.         info += "\nRAM\t\t= " + RAM + "GB";
  188.         info += "\nInternal\t= " + Internal + "TB";
  189.         info += "\nScreen\t\t= " + Layar;
  190.         info += "\nBattery\t\t= " + Battery + " mAH";
  191.        
  192.         return info;
  193.     }
  194. }
  195.  
  196. // RAM.java
  197. public class RAM {
  198.     private int RAM;
  199.    
  200.     public RAM(int RAM) {
  201.         this.RAM = RAM;
  202.     }
  203.    
  204.     public int getRAM() {
  205.         return RAM;
  206.     }
  207. }
  208.  
  209. // CPU.java
  210. public class CPU {
  211.     private String CPU;
  212.    
  213.     public CPU(String CPU) {
  214.         this.CPU = CPU;
  215.     }
  216.    
  217.     public String getCPU() {
  218.         return CPU;
  219.     }
  220. }
  221.  
  222. // Battery.java
  223. public class Battery {
  224.     private int Battery;
  225.     private boolean Started;
  226.     private double Duration;
  227.    
  228.     public Battery(int Battery, double Duration) {
  229.         this.Battery = Battery;
  230.         this.Duration = Duration;
  231.     }
  232.    
  233.     public void powerON() {
  234.         Started = true;
  235.     }
  236.    
  237.     public void powerOFF() {
  238.         Started = false;
  239.     }
  240.    
  241.     public boolean isStarted() {
  242.         return Started;
  243.     }
  244.    
  245.     public void life(double Duration) {
  246.         if(Started) {
  247.             this.Duration += Duration;
  248.         }
  249.        
  250.         else {
  251.             System.out.println("Phone is power off, please hold the button power until on!");
  252.         }
  253.     }
  254.    
  255.     public int getBattery() {
  256.         return Battery;
  257.     }
  258.    
  259.     public double getDuration() {
  260.         return Duration;
  261.     }
  262. }
  263.  
  264. // Internal.java
  265. public class Internal {
  266.     private int Internal;
  267.    
  268.     public Internal(int Internal) {
  269.         this.Internal = Internal;
  270.     }
  271.    
  272.     public int getInternal() {
  273.         return Internal;
  274.     }
  275. }
  276.  
  277. // Type.java
  278. public class Type {
  279.     private String type;
  280.    
  281.     public Type(String type) {
  282.         this.type = type;
  283.     }
  284.    
  285.     public String getType() {
  286.         return type;
  287.     }
  288. }
  289.  
  290. // Lowbatt.java
  291. public class Lowbatt {
  292.     private Phone phone;
  293.    
  294.     public void setPhone(Phone phone) {
  295.         this.phone = phone;
  296.     }
  297.    
  298.     public void showBattery() {
  299.         System.out.println("Battery : " + phone.getBattery());
  300.     }
  301.    
  302. //  public void showStatus() {
  303. //      if(this.phone.getBattery().isStarted()) {
  304. //          System.out.println("Phone is Power ON");
  305. //      }
  306. //     
  307. //  Error Code
  308. //
  309. //      else {
  310. //          System.out.println("Phone is Power OFF");
  311. //      }
  312. //  }
  313. }
  314.  
  315. // Layar.java
  316. public class Layar {
  317.     private String layar;
  318.    
  319.     public Layar(String layar) {
  320.         this.layar = layar;
  321.     }
  322.    
  323.     public String getLayar() {
  324.         return layar;
  325.     }
  326. }
  327.  
  328. // Camera.java
  329. public class Camera {
  330.     private int Camera;
  331.    
  332.     public Camera(int Camera) {
  333.         this.Camera = Camera;
  334.     }
  335.    
  336.     public int getCamera() {
  337.         return Camera;
  338.     }
  339. }
  340.  
  341. // Director.java
  342. public class Director {
  343.     public void constructLowEnd(Builder builder) {
  344.         builder.setType("LOW END");
  345.         builder.setCPU("SNAPDRAGON 660");
  346.         builder.setInternal(1);
  347.         builder.setMemory(8);
  348.         builder.setLayar("IPS");
  349.         builder.setCamera(16);
  350.         builder.setBattery(3200);
  351.     }
  352.    
  353.     public void constructHighEnd(Builder builder) {
  354.         builder.setType("HIGH END");
  355.         builder.setCPU("SNAPDRAGON 845");
  356.         builder.setInternal(1);
  357.         builder.setMemory(16);
  358.         builder.setLayar("AMOLED");
  359.         builder.setCamera(24);
  360.         builder.setBattery(3500);
  361.     }
  362.    
  363.     public void constructFlagship(Builder builder) {
  364.         builder.setType("FLAGSHIP KILLER");
  365.         builder.setCPU("SNAPDRAGON 855");
  366.         builder.setInternal(3);
  367.         builder.setMemory(64);
  368.         builder.setLayar("SUPER AMOLED");
  369.         builder.setCamera(80);
  370.         builder.setBattery(10000);
  371.     }
  372. }
  373.  
  374. // Progress.java
  375. public class Progress {
  376.     public static void main(String[] args) {
  377.         Director director = new Director();
  378.        
  379.         PhoneBuilder builder = new PhoneBuilder();
  380.         director.constructFlagship(builder);
  381.        
  382.         Phone phone = builder.getResult();
  383.         System.out.println("Phone Built : " + phone.getType());
  384.        
  385.         PhoneManualBuilder manualBuilder = new PhoneManualBuilder();
  386.         director.constructFlagship(manualBuilder);
  387.         Manual phoneManual = manualBuilder.getResult();
  388.         System.out.println("\nPhone manual build : \n" + phoneManual.print());
  389.     }
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement