-2

我被要求处理这个将一些客户数据(来自电子商务数据库)导出到自定义格式文本文件的后端计划工作。下面的代码是我发现的。

我只是想把它全部删除,但我不能。我是否有可能在不改变太多的情况下改进它?

public class AConverter implements CustomerConverter {

    protected final Logger LOG = LoggerFactory.getLogger(AConverter.class);

    private final static String SEPARATOR = ";";
    private final static String CR = "\n";

    public String create(Customer customer) {

        if (customer == null)
            return null;

        LOG.info("Exporting customer, uidpk: {}, userid: {}", customer.getUidPk(), customer.getUserId());

        StringBuilder buf = new StringBuilder();

        buf.append("<HEAD>");
        buf.append(SEPARATOR);
        buf.append(String.valueOf(customer.getUidPk()));
        buf.append(SEPARATOR);
        byte[] fullName = null;
        try {
            fullName = customer.getFullName().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e1) {
            fullName = customer.getFullName().getBytes();
        }
        String name = null;
        try {
            name = new String(fullName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            name = customer.getFullName();
        }
        buf.append(limitString(name, 40));
        buf.append(SEPARATOR);
        final CustomerAddress preferredShippingAddress = customer.getPreferredShippingAddress();
        if (preferredShippingAddress != null) {
            final String street1 = preferredShippingAddress.getStreet1();
            if (street1 != null) {
                buf.append(limitString(street1, 40));
            }
        } else {
            buf.append(" ");
        }
        buf.append(SEPARATOR);

        final String addressStr = buildAddressString(customer);
        buf.append(limitString(addressStr, 40));
        buf.append(SEPARATOR);
        buf.append(limitString(customer.getEmail(), 80));
        buf.append(SEPARATOR);
        if (preferredShippingAddress!=null && preferredShippingAddress.getStreet2() != null) {
            buf.append(limitString(preferredShippingAddress.getStreet2(), 40));
        } else {
            buf.append(" ");
        }
        buf.append(SEPARATOR);
        buf.append(limitString(customer.getPhoneNumber(), 25));
        buf.append(SEPARATOR);
        if (preferredShippingAddress!=null) {
            if(preferredShippingAddress.getCountry()!=null) {
                buf.append(preferredShippingAddress.getCountry());
            } else {
                buf.append(" ");
            }
        } else {
            buf.append(" ");
        }
        buf.append(SEPARATOR);
        if (preferredShippingAddress!=null) {
            if(preferredShippingAddress.getCountry()!=null) {
                buf.append(preferredShippingAddress.getCountry());
            } else {
                buf.append(" ");
            }
        } else {
            buf.append(" ");
        }
        buf.append(SEPARATOR);

        String fodselsnummer = " ";
        try {
            Map<String, AttributeValue> profileValueMap = customer.getProfileValueMap();
            AttributeValue attributeValue = profileValueMap.get("CODE");
            fodselsnummer = attributeValue.getStringValue();
        } catch (Exception e) {
        }
        buf.append(fodselsnummer);
        buf.append(CR);
        final String string = buf.toString();

        return string;

    }

    private String buildAddressString(Customer customer) {
        final CustomerAddress preferredShippingAddress = customer.getPreferredShippingAddress();
        if (preferredShippingAddress != null) {
            final String zipOrPostalCode = preferredShippingAddress.getZipOrPostalCode();
            final String city = preferredShippingAddress.getCity();
            if (zipOrPostalCode != null && city != null) {
                return zipOrPostalCode + " " + city;
            } else if(zipOrPostalCode == null && city != null) {
                return city;
            } else if(zipOrPostalCode != null && city == null) {
                return zipOrPostalCode;
            }
        }
        return " ";
    }

    private String limitString(String value, int numOfChars) {
        if (value != null && value.length() > numOfChars)
            return value.substring(0, numOfChars);
        else
            return value;
    }

}
4

1 回答 1

0

你说你想改进它,你想删除它,但你不能。我不确定你为什么不能。我也不明白你为什么要删除它。但这听起来就像我在阅读Martin Fowler 的Refactoring之前的那种态度。我强烈建议你读那本书,如果你还没有的话。

当然可以改进此代码(或任何代码)而无需全部重写。最明显的改进是create通过创建一些实用方法来消除方法中的一些重复代码,然后将create方法分解为几个更小的方法,即模板方法。

create此外,在将客户姓名转换为 UTF-8 字节流,然后再转换为字符串的方法中有一段代码存在问题。我无法想象那是为了什么。最后,如果客户为空,则返回空。这不太可能是必要的或明智的。

为了好玩,我决定对这段代码进行一些重构。(请注意,正确的重构涉及单元测试;我没有对此代码进行任何测试,甚至没有编译下面的代码,更不用说测试了。)这是重写此代码的一种可能方法:

public class AConverter implements CustomerConverter {
    protected final Logger LOG = LoggerFactory.getLogger(AConverter.class);

    private final static String SEPARATOR = ";";
    private final static String CR = "\n";

    public String create(Customer customer) {
        if (customer == null) throw new IllegalArgumentException("no cust");

        LOG.info("Exporting customer, uidpk: {}, userid: {}",
                customer.getUidPk(), customer.getUserId());

        StringBuilder buf = new StringBuilder();
        doHead(buf, customer);
        doAddress(buf, customer);
        doTail(buf, customer);
        return buf.toString();
    }

    private void doHead(StringBuilder buf, Customer customer) {
        append(buf, "<HEAD>");
        append(buf, String.valueOf(customer.getUidPk()));
        append(buf, limitTo(40, customer.getFullName()));
    }

    private void doAddress(StringBuilder buf, Customer customer) {
        append(buf, limitTo(40, street1of(customer)));
        append(buf, limitTo(40, addressOf(customer)));
        append(buf, limitTo(80, customer.getEmail()));
        append(buf, limitTo(40, street2of(customer)));
        append(buf, limitTo(25, customer.getPhoneNumber()));
        append(buf, countryOf(customer));
        append(buf, countryOf(customer));
    }

    private void doTail(StringBuilder buf, Customer customer) {
        buf.append(fodselsnummerOf(customer));
        buf.append(CR);
    }

    private void append(StringBuilder buf, String s) {
        buf.append(s).append(SEPARATOR);
    }

    private String street1of(Customer customer) {
        final CustomerAddress shipto = customer.getPreferredShippingAddress();
        if (shipto == null) return " ";
        if (shipto.getStreet1() != null) return shipto.getStreet1();
        return " ";
    }

    private String street2of(Customer customer) {
        final CustomerAddress shipto = customer.getPreferredShippingAddress();
        if (shipto == null) return " ";
        if (shipto.getStreet2() != null) return shipto.getStreet2();
        return " ";
    }

    private String addressOf(Customer customer) {
        final CustomerAddress shipto = customer.getPreferredShippingAddress();
        if (shipto == null) return " ";

        final String post = preferredShippingAddress.getZipOrPostalCode();
        final String city = preferredShippingAddress.getCity();

        if (post != null && city != null) return post + " " + city;
        if (post == null && city != null) return city;
        if (post != null && city == null) return post;
        return " ";
    }

    private String countryOf(Customer customer) {
        final CustomerAddress shipto = customer.getPreferredShippingAddress();
        if (shipto == null) return " ";
        if (shipto.getCountry() != null) return shipto.getCountry();
        return " ";
    }

    private String limitTo(int numOfChars, String value) {
        if (value != null && value.length() > numOfChars)
            return value.substring(0, numOfChars);
        return value;
    }

    private String fodelsnummerOf(Customer customer) {
        try {
            Map<String, AttributeValue> profileValueMap =
                customer.getProfileValueMap();
            AttributeValue attributeValue = profileValueMap.get("CODE");
            return attributeValue.getStringValue();
        } catch (Exception e) {
            return " ";
        }
    }
}

我还注意到,如果客户数据的任何字段(例如电子邮件地址)中恰好有分号,则自定义格式文本文件的格式存在问题,因为这是您的分隔符。我相信这是一个已知问题?

于 2012-01-25T00:20:08.683 回答