1、类图
2、代码实例
public abstract class ConditionBuilder { public abstract void buildOrderId(); public abstract void buildOrderType(); public abstract SearchCondition getResult(); }
public class SearchCondition {
private String orderId;
private Integer orderType;
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public Integer getOrderType() {
return orderType;
}
public void setOrderType(Integer orderType) {
this.orderType = orderType;
}
public String showSearchCondition(){
return "SearchCondition{" +
"orderId='" + orderId + '\'' +
", orderType=" + orderType +
'}';
}
}
public class SearchConditionBuilder extends ConditionBuilder{
private SearchCondition searchCondition = new SearchCondition();
private Map<String,Object> reqParams;
public SearchConditionBuilder(Map<String, Object> reqParams) {
if(reqParams!=null){
this.reqParams = reqParams;
}else{
this.reqParams= new HashMap<>();
}
}
@Override
public void buildOrderId() {
searchCondition.setOrderId(reqParams.get("orderId")!=null?(String)reqParams.get("orderId"):null);
}
@Override
public void buildOrderType() {
searchCondition.setOrderType(reqParams.get("orderType")!=null?(Integer) reqParams.get("orderType"):null);
}
@Override
public SearchCondition getResult() {
return searchCondition;
}
}
public class ConditionDirector {
public void constructCondition(ConditionBuilder conditionBuilder){
conditionBuilder.buildOrderId();
conditionBuilder.buildOrderType();
}
}
测试代码main():
public class TestBuilder {
public static void main(String[] args) {
ConditionDirector conditionDirector = new ConditionDirector();
Map<String,Object> reqParams= new HashMap<>();
reqParams.put("orderId","123456");
reqParams.put("orderType",0);
SearchConditionBuilder searchConditionBuilder = new SearchConditionBuilder(reqParams);
conditionDirector.constructCondition(searchConditionBuilder);
SearchCondition result = searchConditionBuilder.getResult();
System.out.println(result.showSearchCondition());
}
}
标签:orderId,reqParams,orderType,代码,SearchCondition,案例,void,设计模式,public From: https://www.cnblogs.com/richicewoo/p/16758057.html