关于利用静态内部类来完成一个链式编程的例子

  1 package com.booway.d3tilor.sdk.framework.config;
  2 
  3 import com.booway.d3tilor.sdk.framework.exception.D3tilorException;
  4 
  5 /**
  6  * sdk配置类
  7  * @author lanjun
  8  *
  9  */
 10 public class D3tilorConfig
 11 {
 12     
 13     private D3tilorConfig(D3tilorConfigBuilder builder)
 14     {
 15         if (null == builder.dbDriverClass || builder.dbDriverClass.trim().equals(""))
 16         {
 17             throw new D3tilorException("请通过D3tilorConfigBuilder.jdbc方法设置数据库驱动类!");
 18         }
 19         if (null == builder.dbUrl || builder.dbUrl.trim().equals(""))
 20         {
 21             throw new D3tilorException("请通过D3tilorConfigBuilder.jdbc方法设置数据库连接url!");
 22         }
 23         if (null == builder.dbUser || builder.dbUser.trim().equals(""))
 24         {
 25             throw new D3tilorException("请通过D3tilorConfigBuilder.jdbc方法设置数据库账号!");
 26         }
 27         if (null == builder.dbPwd || builder.dbPwd.trim().equals(""))
 28         {
 29             throw new D3tilorException("请通过D3tilorConfigBuilder.jdbc方法设置数据库密码!");
 30         }
 31         if (null == builder.sevenZExePath || builder.sevenZExePath.trim().equals(""))
 32         {
 33             throw new D3tilorException("请通过D3tilorConfigBuilder.sevenZExePath方法设置7z命令全路径!(windows环境一般以7z.exe结尾,linux环境一般以7za结尾)");
 34         }
 35         if (null == builder.d3ExePath || builder.d3ExePath.trim().equals(""))
 36         {
 37             throw new D3tilorException("请通过D3tilorConfigBuilder.d3ExePath方法设置D3全路径!");
 38         }
 39         if (null == builder.tilesPath || builder.tilesPath.trim().equals(""))
 40         {
 41             throw new D3tilorException("请通过D3tilorConfigBuilder.tilesPath方法设置Tiles工具全路径!");
 42         }
 43         if (null == builder.dataPath || builder.dataPath.trim().equals(""))
 44         {
 45             throw new D3tilorException("请通过D3tilorConfigBuilder.dataPath方法设置数据区路径!");
 46         }
 47         if (null == builder.tempFilePath || builder.tempFilePath.trim().equals(""))
 48         {
 49             throw new D3tilorException("请通过D3tilorConfigBuilder.tempFilePath方法设置临时文件路径!");
 50         }
 51         if (null == builder.oriObjxPath || builder.oriObjxPath.trim().equals(""))
 52         {
 53             throw new D3tilorException("请通过D3tilorConfigBuilder.oriObjxPath方法设置原点objx文件路径!");
 54         }
 55         this.dbDriverClass = builder.dbDriverClass;
 56         this.dbUrl = builder.dbUrl;
 57         this.dbUser = builder.dbUser;
 58         this.dbPwd = builder.dbPwd;
 59         this.sevenZExePath = builder.sevenZExePath;
 60         this.d3ExePath = builder.d3ExePath;
 61         this.tilesPath = builder.tilesPath;
 62         this.dataPath = builder.dataPath;
 63         this.tempFilePath = builder.tempFilePath;
 64         this.timeout = builder.timeout == 0 ? 30 : builder.timeout;
 65         this.oriObjxPath = builder.oriObjxPath;
 66     }
 67     
 68     private final String dbDriverClass;
 69     
 70     private final String dbUrl;
 71     
 72     private final String dbUser;
 73     
 74     private final String dbPwd;
 75     
 76     private final String sevenZExePath;
 77     
 78     private final String d3ExePath;
 79     
 80     private final String tilesPath;
 81     
 82     private final String dataPath;
 83     
 84     private final String oriObjxPath;
 85     
 86     private final String tempFilePath;
 87     
 88     private final int timeout;
 89     
 90     /**
 91      * 获取数据库驱动类
 92      * @return
 93      */
 94     public String getDbDriverClass()
 95     {
 96         return dbDriverClass;
 97     }
 98 
 99     /**
100      * 获取数据库连接url
101      * @return
102      */
103     public String getDbUrl()
104     {
105         return dbUrl;
106     }
107 
108     /**
109      * 获取数据库账号
110      * @return
111      */
112     public String getDbUser()
113     {
114         return dbUser;
115     }
116 
117     /**
118      * 获取数据库密码
119      * @return
120      */
121     public String getDbPwd()
122     {
123         return dbPwd;
124     }
125 
126     /**
127      * 获取7z执行路径
128      * @return
129      */
130     public String getSevenZExePath()
131     {
132         return sevenZExePath;
133     }
134 
135     /**
136      * 获取临时文件路径
137      * @return
138      */
139     public String getTempFilePath()
140     {
141         return tempFilePath;
142     }
143 
144     /**
145      * 获取数据区路径
146      * @return
147      */
148     public String getDataPath()
149     {
150         return dataPath;
151     }
152 
153     /**
154      * 解析超时时间
155      * @return
156      */
157     public int getTimeout()
158     {
159         return timeout;
160     }
161 
162     /**
163      * D3EXE路径
164      * @return
165      */
166     public String getD3ExePath()
167     {
168         return d3ExePath;
169     }
170 
171     /**
172      * tiles工具路径
173      * @return
174      */
175     public String getTilesPath()
176     {
177         return tilesPath;
178     }
179 
180     /**
181      * 原点objx文件路径
182      * @return
183      */
184     public String getOriObjxPath()
185     {
186         return oriObjxPath;
187     }
188 
189     /**
190      * 配置类构造器
191      * @author yhh
192      *
193      */
194     public static class D3tilorConfigBuilder
195     {
196         private String dbDriverClass;
197         
198         private String dbUrl;
199         
200         private String dbUser;
201         
202         private String dbPwd;
203         
204         private String sevenZExePath;
205         
206         private String d3ExePath;
207         
208         private String tilesPath;
209         
210         private String tempFilePath;
211         
212         private String dataPath;
213         
214         private String oriObjxPath;
215         
216         private int timeout;
217         
218         /**
219          * 数据源参数
220          * @param dbDriverClass
221          * @param dbUrl
222          * @param dbUser
223          * @param dbPwd
224          * @return
225          */
226         public D3tilorConfigBuilder jdbc(String dbDriverClass, String dbUrl, String dbUser, String dbPwd)
227         {
228             this.dbDriverClass = dbDriverClass;
229             this.dbUrl = dbUrl;
230             this.dbUser = dbUser;
231             this.dbPwd = dbPwd;
232             return this;
233         }
234         
235         /**
236          * 7z执行全路径
237          * @param sevenZExePath
238          * @return
239          */
240         public D3tilorConfigBuilder sevenZExePath(String sevenZExePath)
241         {
242             this.sevenZExePath = sevenZExePath;
243             return this;
244         }
245         
246         /**
247          * 数据区路径
248          * @param dataPath
249          * @return
250          */
251         public D3tilorConfigBuilder dataPath(String dataPath)
252         {
253             this.dataPath = dataPath;
254             return this;
255         }
256         
257         /**
258          * 临时文件路径
259          * @param tempFilePath
260          * @return
261          */
262         public D3tilorConfigBuilder tempFilePath(String tempFilePath)
263         {
264             this.tempFilePath = tempFilePath;
265             return this;
266         }
267         
268         /**
269          * 解析超时时间(单位:分钟)
270          * @param timeout
271          * @return
272          */
273         public D3tilorConfigBuilder timeout(int timeout)
274         {
275             this.timeout = timeout;
276             return this;
277         }
278         
279         /**
280          * D3EXE路径
281          * @param d3ExePath
282          * @return
283          */
284         public D3tilorConfigBuilder d3ExePath(String d3ExePath)
285         {
286             this.d3ExePath = d3ExePath;
287             return this;
288         }
289         
290         /**
291          * Tiles工具路径
292          * @param tilesPath
293          * @return
294          */
295         public D3tilorConfigBuilder tilesPath(String tilesPath)
296         {
297             this.tilesPath = tilesPath;
298             return this;
299         }
300         
301         /**
302          * 原点objx路径
303          * @param oriObjxPath
304          * @return
305          */
306         public D3tilorConfigBuilder oriObjxPath(String oriObjxPath)
307         {
308             this.oriObjxPath = oriObjxPath;
309             return this;
310         }
311         
312         /**
313          * 构造配置类对象
314          * @return
315          */
316         public D3tilorConfig build()
317         {
318             return new D3tilorConfig(this);
319         }
320     }
321     
322 }

使用如下,就可以实现链式编程的效果了

1 D3tilorContext.getContext().init(new D3tilorConfig.D3tilorConfigBuilder()
2                 .jdbc("com.mysql.jdbc.Driver", "jdbc:mysql://10.1.19.9:3306/mysql?useUnicode=true&characterEncoding=UTF8&autoReconnect=true"
3                         , "root", "200717")
4                 .sevenZExePath("D:\dailySoftInstall\7-Zip\7z.exe").d3ExePath("D:\博微软件\D3Station\D3Station.exe").tilesPath("D:\博微软件\scene2tiles\Release\scene2tiles.exe")
5                 .tempFilePath("D:\tmp").dataPath("D:\gimdata").oriObjxPath("1111").build());
原文地址:https://www.cnblogs.com/zyfBlogShare/p/13723377.html