Adapter模式:
public interface IRoundPeg{
public void insertIntoHole(String msg);
}
public interface ISquarePeg{
public void insert(String str);
}
public class RoundPeg implements IRoundPeg{
public void insertIntoHole(String msg) {
System.out.println("RoundPeg insertIntoHole(): "+msg);
}
}
public class SquarePeg implements ISquarePeg{
public void insert(String str){
System.out.println("SquarePeg insert(): "+str);
}
}
public class PegAdapter implements IRoundPeg,ISquarePeg{
private RoundPeg roundPeg;
private SquarePeg squarePeg;
public PegAdapter(RoundPeg peg){
this.roundPeg=peg;
}
public PegAdapter(SquarePeg peg){
this.squarePeg=peg;
}
public void insertIntoHole(String msg) {
System.out.println("SquarePeg insert(): "+msg);
}
public void insert(String str) {
System.out.println("RoundPeg insertIntoHole(): "+str);
}
}
public class Test {
public static void main(String[] args) {
SquarePeg sp=new SquarePeg();
PegAdapter pa=new PegAdapter(sp);
pa.insert("Square----------"); //各调用各自实现的方法
}
}