LowRider
to Adaptee?
PoliceCar
to AdapterImplementation?
PrivilegedCar
to Adapter?
package Adapter;
public interface PrivilegedCar {
public void moveWithSiren();
public void moveWithoutSiren();
}
package Adapter;
public class PoliceCar implements PrivilegedCar{
private LowRider car;
public PoliceCar(){
this.car = new LowRider();
}
public void moveWithSiren(){
System.out.println("*biu biu!* GETOUTA ROAD! *biu biu!* ");
}
public void moveWithoutSiren(){
car.moveSlowlyInTheHood();
}
}
package Adapter;
public class LowRider {
public void moveSlowlyInTheHood(){
System.out.println("havin a cool ride, dawg...");
}
public void runAwayFromPolice(){
System.out.println("shieeeet... the cops! run!");
}
}
package Adapter;
public class AppMain {
public static void main(String[] args) {
PoliceCar pc = new PoliceCar();
pc.moveWithoutSiren();
pc.moveWithSiren();
}
Jeśli tak to czy nie lepiej zrobić tak:
package Adapter;
public interface CityVehicle {
default public void moveSlowlyInTheHood(){
System.out.println("havin a cool ride, dawg...");
}
}
package Adapter;
public interface PrivilegedCar {
public void moveWithSiren();
public void moveWithoutSiren();
}
package Adapter;
public class PoliceCar implements PrivilegedCar, CityVehicle{
public PoliceCar(){
}
public void moveWithSiren(){
System.out.println("*biu biu!* GETOUTA ROAD! *biu biu!* ");
}
public void moveWithoutSiren(){
moveSlowlyInTheHood();
}
}
package Adapter;
public class LowRider implements CityVehicle{
public void runAwayFromPolice(){
System.out.println("shieeeet... the cops! run!");
}
}
package Adapter;
public class AppMain {
public static void main(String[] args) {
PoliceCar pc = new PoliceCar();
pc.moveWithoutSiren();
pc.moveWithSiren();
}
}