public interface Bomb {
void bomb();
}
public interface Phone {
void start();
void shutdown();
}
public class CommonPhone implements Phone{
@Override
public void start() {
System.out.println("Phone Start!");
}
@Override
public void shutdown() {
System.out.println("Phone Shutdown!");
}
}
public class SamsungAdapter implements Phone,Bomb {
@Override
public void bomb() {
System.out.println("Bom!!!HAHAHA!!!");
}
@Override
public void start() {
System.out.println("Samsung Start!");
}
@Override
public void shutdown() {
System.out.println("Samsung Shutdown!");
}
}
public class SamsungAdapterByObject implements Bomb {
private Phone phone;
public SamsungAdapterByObject(Phone p){
this.phone=p;
}
@Override
public void bomb() {
System.out.println("Bom!!!HAHAHA!!!");
}
public void start() {
phone.start();
}
public void shutdown() {
phone.shutdown();
}
}
public class App {
public static void main(String[] args) {
Phone apple=new CommonPhone();
apple.start();
apple.shutdown();
* 适配器模式有两种实现方式,一种是继承,一种是组合
*/
SamsungAdapter samSung=new SamsungAdapter();
samSung.start();
samSung.shutdown();
samSung.bomb();
SamsungAdapterByObject samSungByObject=new SamsungAdapterByObject(new CommonPhone());
samSungByObject.start();
samSungByObject.shutdown();
samSungByObject.bomb();
}
}