适配器模式概念
适配器模式(Adapter Pattern):将一个接口转换成客户希望的另一个接口,使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper)。适配器模式既可以作为类结构型模式,也可以作为对象结构型模式。
在适配器模式中,我们通过增加一个新的适配器类来解决接口不兼容的问题,使得原本没有任何关系的类可以协同工作。
根据适配器类与适配者类的关系不同,适配器模式可分为对象适配器和类适配器两种,在对象适配器模式中,适配器与适配者之间是关联关系;在类适配器模式中,适配器与适配者之间是继承(或实现)关系。
类适配器实际应用
Customer期望能通过调用AddressValidator接口isValidAddress方法,从而实现调用USAddress/CAAddress类的验证方法(实现不同验证器的统一调度)。
CAAddress是原来老系统里就存在的类,不能把方法名isValidCanadianAddress直接改为isValidAddress。否者会影响老系统的使用
这时就需要一个适配器CAAddressAdapter继承CAAddress,并且实现接口isValidAddress
Customer的里根据入参来判断是实例化CAAddressAdapter还是USAddress
package com.lzhsite.technology.designPattern.adapter.Address; public class Customer { public static final String US = "US"; public static final String CANADA = "CANADA"; private String address; private String name; private String zip; private String state; private String type; public Customer(String address, String name, String zip, String state, String type) { this.address = address; this.name = name; this.zip = zip; this.state = state; this.type = type; } public boolean isValidAddress() { //Customer期望能通过调用AddressValidator接口isValidAddress方法,从而实现调用USAddress/CAAddress类的验证方法(实现不同验证器的统一调度)。 //CAAddress是原来老系统里就存在的类,不能把方法名isValidCanadianAddress直接改为isValidAddress。否者会影响老系统的使用 //这时就需要一个适配器CAAddressAdapter继承CAAddress,并且实现接口isValidAddress //Customer的里根据入参来判断是实例化CAAddressAdapter还是USAddress AddressValidator addressValidator = getValidator(type); return addressValidator.isValidAddress(address, zip, state); } private AddressValidator getValidator(String customerType) { AddressValidator addressValidator = null; if (customerType.equals(Customer.US)) { addressValidator = new USAddress(); } if (customerType.equals(Customer.CANADA)) { addressValidator = new CAAddressAdapter(); } return addressValidator; } }
package com.lzhsite.technology.designPattern.adapter.Address; public interface AddressValidator { public boolean isValidAddress(String address, String zip, String state); }
package com.lzhsite.technology.designPattern.adapter.Address; public class USAddress implements AddressValidator { public boolean isValidAddress(String address, String zip, String state) { if (address.trim().length() < 10) { return false; } if (zip.trim().length() < 5 || zip.trim().length() > 10) { return false; } if (state.trim().length() != 2) { return false; } return true; } }
package com.lzhsite.technology.designPattern.adapter.Address; public class CAAddressAdapter extends CAAddress implements AddressValidator { public boolean isValidAddress(String address, String zip, String state) { return isValidCanadianAddress(address, zip, state); } }
package com.lzhsite.technology.designPattern.adapter.Address; public class CAAddress { public boolean isValidCanadianAddress(String address, String pcode, String prvnc) { if (address.trim().length() < 15) { return false; } if (pcode.trim().length() != 6) { return false; } if (prvnc.trim().length() < 6) { return false; } return true; } }
package com.lzhsite.technology.designPattern.adapter.Address; public class CAAddress { public boolean isValidCanadianAddress(String address, String pcode, String prvnc) { if (address.trim().length() < 15) { return false; } if (pcode.trim().length() != 6) { return false; } if (prvnc.trim().length() < 6) { return false; } return true; } }
适配器模式的总结