Advertisement
Guest User

ExemploAdapter

a guest
Oct 9th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) throws Exception {
  5.         // Your code here!
  6.        
  7.         Veiculo bugattiVeyron = new BugattiVeyron();
  8.         VeiculoAdapter bugattiVeyronAdapter = new VeiculoAdapterImpl(bugattiVeyron);
  9.        
  10.         System.out.println(bugattiVeyronAdapter.getSpeed());
  11.    
  12.     }
  13. }
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. public interface Veiculo {
  21.     // retorna a velocidade em MPH
  22.     double getSpeed();
  23. }
  24.  
  25.  
  26.  
  27. public class BugattiVeyron implements Veiculo {
  28.  
  29.     @Override
  30.     public double getSpeed() {
  31.         return 268;
  32.     }
  33.    
  34. }
  35.  
  36.  
  37. public interface VeiculoAdapter {
  38.     // retorna a valocidade em KM/H
  39.     double getSpeed();
  40. }
  41.  
  42.  
  43.  
  44.  
  45. public class VeiculoAdapterImpl implements VeiculoAdapter {
  46.     private Veiculo luxuryCars;
  47.      
  48.     public VeiculoAdapterImpl(Veiculo luxuryCars){
  49.         this.luxuryCars = luxuryCars;
  50.     }
  51.  
  52.     @Override
  53.     public double getSpeed() {
  54.         return convertMPHtoKMPH(luxuryCars.getSpeed());
  55.     }
  56.      
  57.     private double convertMPHtoKMPH(double mph) {
  58.         return mph * 1.60934;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement