首页 > 其他分享 >设计模式之建造者模式-案例代码

设计模式之建造者模式-案例代码

时间:2022-10-06 17:34:07浏览次数:48  
标签:orderId reqParams orderType 代码 SearchCondition 案例 void 设计模式 public

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

相关文章

  • 设计模式 创建型
    Java设计模式学习本文作者:MrBird本文链接:http://mrbird.cc/Java设计模式.html本文仅为个人学习整理转载创建型模式简单工厂模式简单工厂模式严格意义上来说,并不......
  • 利用TRansformer进行端到端的目标检测及跟踪(附源代码)
    计算机视觉研究院专栏作者:Edison_G现存的用检测跟踪的方法采用简单的heuristics,如空间或外观相似性。这些方法,尽管其共性,但过于简单,不足以建模复杂的变化,如通过遮挡跟踪。公......
  • 代码在内存单元中的分布情况
    堆区地址增长,未初始化:默认cdcdcdcd栈区地址减小  未初始化:默认cccccccc全局初始化区(GVAR): 全局未初始化区(bss)(默认给0值):全局静态(static)初始化区:te......
  • Swim-Transform V2:用于目标检测,视觉大模型不再是难题(附源代码)
    公众号ID|ComputerVisionGzq​论文地址:​​https://arxiv.org/pdf/2111.09883.pdf​​源代码:​​https://github.com/microsoft/Swin-Transformer​​​计算机视觉研究院专栏......
  • Yolo的巅峰框架:高效更精确的目标检测框架(附源代码)
    ​计算机视觉研究院专栏作者:Edison_GYOLOX在YOLO系列的基础上做了许多有意思的工作,其主要贡献在于:在YOLOV3的基础上,引入了“DecoupledHead”,“DataAugmentation”,“Anchor......
  • 设计模式之备忘录模式
    备忘录模式概念Memento isabehavioraldesignpatternthatletsyousaveandrestorethepreviousstateofanobjectwithoutrevealingthedetailsofits i......
  • 用伪代码实现进制转化
    伪代码Write"Enterthenewbase"ReadnewBaseWrite"Enterthenumbertobeconverted"ReaddecimalNumberSetquotientto1WHILE(quotientisnotzero)Setq......
  • 014——static应用知识:单例设计模式
    static应用知识:单例设计模式设计模式开发中经常遇到一些问题,一个问题通常有n种解法的,但其中肯定有一种解法是最优的,这个最优的解法被人总结出来了,称之为设计模式。......
  • 关键字,缩进,代码块,
    判断语句(JudgmentStatement)是程序的三大语句结构之一。它的作用是根据判断的条件是否成立(真或假),来决定后续代码执行顺序。if后的判断条件通常是一个布尔表达式。即该表......
  • 设计模式之中介者模式
    中介者模式概念Mediator isabehavioraldesignpatternthatletsyoureducechaoticdependenciesbetweenobjects.Thepatternrestrictsdirectcommunication......