经典代码逻辑:
public class Adapter extends ... implements ...;
package wwx.test;
//适配器
//验证接口
interface ValidateHandler{
void validate();
}
//目前有一个简单验证实现
class EasyValidate implements ValidateHandler{
public void validate() {
// TODO Auto-generated method stub
System.err.println("基础验证");
}
}
class Person{
public ValidateHandler validateHandler;
public void Validate(){
validateHandler.validate();
}
}
//其他的jar包有一个复杂的验证,但是这个jar你改不了,你怎么用呢
class ComplexValidate{
public void complexValidate(){
System.err.println("复杂验证");
}
}
//那就创建一个适配器,继承为了方法的调用,实现为了对应Person的参数定义
class ComplexValidateAdapter extends ComplexValidate implements ValidateHandler {
@Override
public void validate() {
// TODO Auto-generated method stub
super.complexValidate();
}
}
public class Test {
public static void main(String[] args) {
Person p = new Person();
p.validateHandler = new EasyValidate();
p.Validate();
p.validateHandler = new ComplexValidateAdapter();
p.Validate();
}
}