如何解决构造函数参数过多?
1. 构建builder 解决参数过多问题
[java] view plain copy print ?
- package com.polycom.ngma.core.shared.services;
- public class NgmaServiceControlRequest
- {
- String uuid;
- int port;
- String serviceTypeName;
- String startupCommandTemplate;
- String stopCommandTemplate;
- String command; // start, stop
- public static class Builder {
- String uuid;
- int port;
- String serviceTypeName;
- String startupCommandTemplate;
- String stopCommandTemplate;
- String command; // start, stop
- public Builder withUUID(String uuid){
- this.uuid=uuid;
- return this;
- }
- public Builder withPort(int port){
- this.port=port;
- return this;
- }
- public Builder withServiceTypeName(String serviceTypeName){
- this.serviceTypeName=serviceTypeName;
- return this;
- }
- public Builder withstartupCommandTemplate(String startupCommandTemplate){
- this.startupCommandTemplate=startupCommandTemplate;
- return this;
- }
- public Builder withstopCommandTemplate(String stopCommandTemplate){
- this.stopCommandTemplate=stopCommandTemplate;
- return this;
- }
- public Builder withCommand(String command){
- this.command=command;
- return this;
- }
- // client doesn't get to instantiate ngmaservicerequest directly
- public NgmaServiceControlRequest build(){
- return new NgmaServiceControlRequest(this);
- }
- }
- public NgmaServiceControlRequest(Builder builder) {
- this.uuid = builder.uuid;
- this.port = builder.port;
- this.serviceTypeName =builder.serviceTypeName;
- this.startupCommandTemplate =builder.startupCommandTemplate;
- this.stopCommandTemplate =builder.stopCommandTemplate;
- this.command =builder.command;
- }
- public String getUuid()
- {
- return uuid;
- }
- public void setUuid(String uuid)
- {
- this.uuid = uuid;
- }
- public String getCommand()
- {
- return command;
- }
- public void setCommand(String command)
- {
- this.command = command;
- }
- public int getPort() {
- return port;
- }
- public void setPort(int port) {
- this.port = port;
- }
- public String getStartupCommandTemplate() {
- return startupCommandTemplate;
- }
- public void setStartupCommandTemplate(String startupCommandTemplate) {
- this.startupCommandTemplate = startupCommandTemplate;
- }
- public String getStopCommandTemplate() {
- return stopCommandTemplate;
- }
- public void setStopCommandTemplate(String stopCommandTemplate) {
- this.stopCommandTemplate = stopCommandTemplate;
- }
- public String getServiceTypeName() {
- return serviceTypeName;
- }
- public void setServiceTypeName(String serviceTypeName) {
- this.serviceTypeName = serviceTypeName;
- }
- @Override
- public String toString()
- {
- return "NgmaServiceControlRequest [uuid=" + uuid + ", command=" + command + "]";
- }
- }
2. client 调用
[java] view plain copy print ?
- NgmaServiceControlRequest serviceRequest=new NgmaServiceControlRequest.Builder().withUUID(serviceUuid)
- .withPort(internalPort)
- .withServiceTypeName(serviceTypeName)
- .withstartupCommandTemplate(startupCommandTemplate)
- .withstopCommandTemplate(stopCommandTemplate)
- .withCommand(requestType)
- .build();
3. 优点
a 参数变更不影响client调用
b 必须参数可以利用builder 构造函数强制输入,可选参数利用withXXX方法注入。
c 相对于Java bean 不存在多线程情况下set 多次调用失败
d 封装具体对象,client不直接接触,通过builder进行构造
转自http://blog.csdn.net/hanruikai/article/details/44156129
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!