Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
package com.DesignPattern.Structural.Adapter;
public class Adapter extends Adaptee implements Target {
@Override
public void request() {
super.specificRequest();
}
}
package com.DesignPattern.Structural.Adapter;
public class Client {
public static void main(String[] args){
//适配器模式应用
Target target=new Adapter();
target.request();
}
}
适配器模式的实例
Dumplings.java
package com.DesignPattern.Structural.Adapter;
public class Dumplings {
public void makeDumplings(){
System.out.println("调馅");
System.out.println("擀皮");
System.out.println("包馅");
}
}
Hundun.java
package com.DesignPattern.Structural.Adapter;
public interface Hundun {
public void makeHundun();
}
FoodAdapter.java
package com.DesignPattern.Structural.Adapter;
public class FoodAdapter extends Dumplings implements Hundun {
@Override
public void makeHundun() {
super.makeDumplings();
System.out.println("混沌和水饺一样都是用面包馅的食品");
}
}
ClientDemo.java
package com.DesignPattern.Structural.Adapter;
public class ClientDemo {
public static void main(String[] args){
Hundun h=new FoodAdapter();
h.makeHundun();
}
}
适配器模式 Adapter Pattern 变压器模式Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.将一个类的接口变换