Advertisement
rafibatam

Design Pattern Bridge JAVA

Mar 30th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. Bridge Pattern digunakan untuk memisahkan interface dari implementasinya yang akan memberikan fleksibilitas sehingga keduanya dapat bervariasi secara independen. Pattern ini melibatkan sebuah interface yang bertindak sebagai jembatan yang akan membuat fungsionalitas concrete class menjadi independen dari implementasinya. Bridge Pattern termasuk kedalam Structural Pattern.
  2.  
  3. // Engine.java
  4. public interface Engine {
  5.     boolean isStarted();
  6.     void on();
  7.     void off();
  8.     int getFuel();
  9.     void setFuel(int gasolineBar);
  10.     void printStatus();
  11. }
  12.  
  13. // Car.java
  14. public class Car implements Engine {
  15.     private boolean started = false;
  16.     private int fuel = 100;
  17.  
  18.     @Override
  19.     public boolean isStarted() {
  20.         return started;
  21.     }
  22.  
  23.     @Override
  24.     public void on() {
  25.         started = true;
  26.     }
  27.  
  28.     @Override
  29.     public void off() {
  30.         started = false;
  31.     }
  32.  
  33.     @Override
  34.     public int getFuel() {
  35.         return fuel;
  36.     }
  37.  
  38.     @Override
  39.     public void setFuel(int fuel) {
  40.         if(fuel > 0) {
  41.             this.fuel = 100;
  42.         }
  43.        
  44.         else if(fuel < 0) {
  45.             this.fuel = 0;
  46.         }
  47.        
  48.         else {
  49.             this.fuel = fuel;
  50.         }
  51.     }
  52.  
  53.     @Override
  54.     public void printStatus() {
  55.         System.out.println("I'm a Hyper Car");
  56.         System.out.println("My engine is " + (started? "on" : "off"));
  57.         System.out.println("Tank of liquids is : " + fuel + "\n");
  58.     }
  59. }
  60.  
  61. // Motorcycle.java
  62. public class Motorcycle implements Engine {
  63.     private boolean started = false;
  64.     private int fuel = 100;
  65.  
  66.     @Override
  67.     public boolean isStarted() {
  68.         return started;
  69.     }
  70.  
  71.     @Override
  72.     public void on() {
  73.         started = true;
  74.     }
  75.  
  76.     @Override
  77.     public void off() {
  78.         started = false;
  79.     }
  80.  
  81.     @Override
  82.     public int getFuel() {
  83.         return fuel;
  84.     }
  85.  
  86.     @Override
  87.     public void setFuel(int fuel) {
  88.         if(fuel > 0) {
  89.             this.fuel = 100;
  90.         }
  91.        
  92.         else if(fuel < 0) {
  93.             this.fuel = 0;
  94.         }
  95.        
  96.         else {
  97.             this.fuel = fuel;
  98.         }
  99.     }
  100.  
  101.     @Override
  102.     public void printStatus() {
  103.         System.out.println("I'm a Sport Motorcycle");
  104.         System.out.println("My engine is " + (started? "on" : "off"));
  105.         System.out.println("Tank of liquids is : " + fuel);
  106.     }
  107. }
  108.  
  109. // GasolineBar.java
  110. public interface GasolineBar {
  111.     void engine();
  112.     void fuelGasoline();
  113.     void emptyGasoline();
  114. }
  115.  
  116. // GasolineBar.java
  117. public class TankLiquid implements GasolineBar {
  118.     protected Engine engine;
  119.    
  120.     public TankLiquid() {}
  121.    
  122.     public TankLiquid(Engine engine) {
  123.         this.engine = engine;
  124.     }
  125.  
  126.     @Override
  127.     public void fuelGasoline() {
  128.         System.out.println("Gasoline is Fuel");
  129.         engine.setFuel(engine.getFuel() + 0);
  130.         System.out.println("You can go wherever you want");
  131.     }
  132.  
  133.     @Override
  134.     public void emptyGasoline() {
  135.         System.out.println("Gasoline is Empty");
  136.         engine.setFuel(engine.getFuel() + 0);
  137.         System.out.println("You must refill Gasoline!");
  138.     }
  139.  
  140.     @Override
  141.     public void engine() {
  142.         if(engine.isStarted()) {
  143.             engine.off();
  144.         }
  145.        
  146.         else {
  147.             engine.on();
  148.         }
  149.     }
  150. }
  151.  
  152. // Progress.java
  153. public class Progress {
  154.     public static void main(String[] args) {
  155.         Transportation(new Car());
  156.         Transportation(new Motorcycle());
  157.     }
  158.  
  159.     private static void Transportation(Engine engine) {
  160.         System.out.println("Test new engine with fuel tank");
  161.         TankLiquid tankLiquid = new TankLiquid(engine);
  162.         tankLiquid.engine();
  163.         engine.printStatus();
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement