如何解决构造函数参数过多?

1.  构建builder 解决参数过多问题


[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.polycom.ngma.core.shared.services;  
  2.   
  3. public class NgmaServiceControlRequest  
  4. {  
  5.     String uuid;  
  6.     int port;  
  7.     String serviceTypeName;  
  8.     String startupCommandTemplate;  
  9.     String stopCommandTemplate;  
  10.     String command;     // start, stop  
  11.       
  12.       
  13.     public static class Builder {  
  14.           
  15.         String uuid;  
  16.         int port;  
  17.         String serviceTypeName;  
  18.         String startupCommandTemplate;  
  19.         String stopCommandTemplate;  
  20.         String command;     // start, stop  
  21.           
  22.           
  23.           
  24.         public Builder withUUID(String uuid){  
  25.             this.uuid=uuid;  
  26.             return this;  
  27.         }  
  28.           
  29.         public Builder withPort(int port){  
  30.             this.port=port;  
  31.             return this;  
  32.         }  
  33.           
  34.         public Builder withServiceTypeName(String serviceTypeName){  
  35.             this.serviceTypeName=serviceTypeName;  
  36.             return this;  
  37.         }  
  38.           
  39.         public Builder withstartupCommandTemplate(String startupCommandTemplate){  
  40.             this.startupCommandTemplate=startupCommandTemplate;  
  41.             return this;  
  42.         }  
  43.           
  44.         public Builder withstopCommandTemplate(String stopCommandTemplate){  
  45.             this.stopCommandTemplate=stopCommandTemplate;  
  46.             return this;  
  47.         }  
  48.         public Builder withCommand(String command){  
  49.             this.command=command;  
  50.             return this;  
  51.         }  
  52.        // client doesn't get to instantiate ngmaservicerequest directly  
  53.         public NgmaServiceControlRequest build(){  
  54.             return new NgmaServiceControlRequest(this);   
  55.               
  56.         }  
  57.     }  
  58.   
  59.     public NgmaServiceControlRequest(Builder builder) {  
  60.         this.uuid = builder.uuid;  
  61.         this.port = builder.port;  
  62.         this.serviceTypeName =builder.serviceTypeName;  
  63.         this.startupCommandTemplate =builder.startupCommandTemplate;  
  64.         this.stopCommandTemplate =builder.stopCommandTemplate;  
  65.         this.command =builder.command;  
  66.     }  
  67.     public String getUuid()  
  68.     {  
  69.         return uuid;  
  70.     }  
  71.     public void setUuid(String uuid)  
  72.     {  
  73.         this.uuid = uuid;  
  74.     }  
  75.     public String getCommand()  
  76.     {  
  77.         return command;  
  78.     }  
  79.     public void setCommand(String command)  
  80.     {  
  81.         this.command = command;  
  82.     }  
  83.       
  84.     public int getPort() {  
  85.         return port;  
  86.     }  
  87.   
  88.     public void setPort(int port) {  
  89.         this.port = port;  
  90.     }  
  91.   
  92.   
  93.     public String getStartupCommandTemplate() {  
  94.         return startupCommandTemplate;  
  95.     }  
  96.     public void setStartupCommandTemplate(String startupCommandTemplate) {  
  97.         this.startupCommandTemplate = startupCommandTemplate;  
  98.     }  
  99.     public String getStopCommandTemplate() {  
  100.         return stopCommandTemplate;  
  101.     }  
  102.     public void setStopCommandTemplate(String stopCommandTemplate) {  
  103.         this.stopCommandTemplate = stopCommandTemplate;  
  104.     }  
  105.     public String getServiceTypeName() {  
  106.         return serviceTypeName;  
  107.     }  
  108.     public void setServiceTypeName(String serviceTypeName) {  
  109.         this.serviceTypeName = serviceTypeName;  
  110.     }  
  111.     @Override  
  112.     public String toString()  
  113.     {  
  114.         return "NgmaServiceControlRequest [uuid=" + uuid + ", command=" + command + "]";  
  115.     }  
  116.   
  117.   
  118.       
  119. }  

2.  client 调用


[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. NgmaServiceControlRequest serviceRequest=new NgmaServiceControlRequest.Builder().withUUID(serviceUuid)  
  2.                     .withPort(internalPort)  
  3.                     .withServiceTypeName(serviceTypeName)  
  4.                     .withstartupCommandTemplate(startupCommandTemplate)  
  5.                     .withstopCommandTemplate(stopCommandTemplate)  
  6.                     .withCommand(requestType)  
  7.                     .build();  

3. 优点

  a 参数变更不影响client调用

  b  必须参数可以利用builder 构造函数强制输入,可选参数利用withXXX方法注入。

  c 相对于Java bean  不存在多线程情况下set 多次调用失败

  d 封装具体对象,client不直接接触,通过builder进行构造


转自http://blog.csdn.net/hanruikai/article/details/44156129

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值