在这种特定情况下,由于除了这三个字符串之外的所有代码,代码在本质上都是相同的,因此对于每个要构建的字符串,我都会有一个映射条目:
Map<String,String> map = new HashMap<String,String>();map.put("EditorialOffice","");map.put("EditorialBoard","");map.put("AdvisoryBoard","");// could make constants for above Strings, or even an enum然后将您的代码更改为以下内容
if(eventType == XmlPullParser.START_TAG) { soapResponse= xpp.getName().toString(); String current = map.get(soapResponse); if (current != null && xpp.getText()!=null) { map.put( soapResponse, current += xpp.getText()); } eventType = xpp.next();}没有“如果……那么……否则”。甚至没有为策略模式等增加多个类的复杂性。地图是您的朋友。在某些情况下,策略固然很好,但这一策略非常简单,无需解决。



