链式编程

Posted yweihainan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了链式编程相关的知识,希望对你有一定的参考价值。

 1 package com.nd.pdca.model;
 2 
 3 public class Manager {
 4 
 5     private String id;
 6     private String userId;
 7     private String userNo;
 8     private String userName;
 9     private String unitId;
10 
11     public String getUserNo() {
12         return userNo;
13     }
14 
15     public void setUserNo(String userNo) {
16         this.userNo = userNo;
17     }
18 
19     public String getId() {
20         return id;
21     }
22 
23     public void setId(String id) {
24         this.id = id;
25     }
26 
27     public String getUnitId() {
28         return unitId;
29     }
30 
31     public void setUnitId(String unitId) {
32         this.unitId = unitId;
33     }
34 
35     public String getUserId() {
36         return userId;
37     }
38 
39     public void setUserId(String userId) {
40         this.userId = userId;
41     }
42 
43     public String getUserName() {
44         return userName;
45     }
46 
47     public void setUserName(String userName) {
48         this.userName = userName;
49     }
50 
51     public static Builder create() {
52         return new Builder();
53     }
54     
55     public static class Builder{
56         private String id;
57         private String userId;
58         private String userNo;
59         private String userName;
60         private String unitId;
61         
62         public Builder id(String id){
63             this.id = id;
64             return this;
65         }  
66         
67         public Builder userId(String userId){
68             this.userId = userId;
69             return this;
70         }  
71         
72         public Builder userNo(String userNo){
73             this.userNo = userNo;
74             return this;
75         }  
76         
77         public Builder userName(String userName){
78             this.userName = userName;
79             return this;
80         }        
81         
82         public Builder unitId(String unitId){
83             this.unitId = unitId;
84             return this;
85         }
86         
87         public Manager build(){
88             Manager manager = new Manager();
89             manager.setId(id);
90             manager.setUserName(userName);
91             manager.setUserId(userId);
92             manager.setUserNo(userNo);
93             manager.setUnitId(unitId);
94             return manager;
95         }
96     }
97 }
1  Manager manager = Manager.create().id(id).username(username).userId(userId).userNo(userNo).unitId(unitId).build();
2 
3 //  这样的链式编程就不需要调用set/get方法的形式了,使得在new的地方代码更为简洁好看。

 

构造Map

 1 public class ParamBuilder {
 2 
 3     private Map<String, Object> param = new HashMap<>(5);
 4 
 5     public ParamBuilder withParam(String paramName, Object paramValue) {
 6         param.put(paramName, paramValue);
 7         return this;
 8     }
 9 
10     public Map<String, Object> build() {
11         return param;
12     }
13 }

 

以上是关于链式编程的主要内容,如果未能解决你的问题,请参考以下文章

java链式编程/级联式编程

链式编程:泛型实现的扩展方法类

链式编程

jQuery 链式编程

Python函数

[jQuery]jQuery链式编程