As shown in figure..
CodePudding user response:
Cannot treat as the same, see specific requirements,And in terms of object-oriented encapsulation features, no matter have constructor parameter, provide the get/set is meaningful, because with method can more secure protection properties, such as the judge whether effective are allowed to set properties
CodePudding user response:
It depends, there is no strong requirements must be used to construct and get/set, developing are commonly use get/setCodePudding user response:
Get set object domain is dynamic, a bit: flexible, disadvantages: some scene will appear concurrency issues.Pure constructor assignment, the generated object domain is static, there will be no concurrency issues, disadvantages: not flexible, low readability if parameters, constructors have the overloading requirement tedious
So how to guarantee the flexible object domain assignment, readable and can guarantee the domain static, builder model can solve the problem of the
@ Getter
Public class ResponseEntity
Private String status;
private String msg;
Private T data;
Private ResponseEntity (Builder Builder) {
Enclosing the status=builder. The status;
This. MSG=builder. MSG;
This data=https://bbs.csdn.net/topics/builder.data;
}
Public class Builder {
Private String status;
private String msg;
Private T data;
Public Builder status (String status) {
Enclosing the status=status;
Return this.
}
Public Builder MSG (String MSG) {
this.msg=msg;
Return this.
}
Public Builder data (data) T {
This data=https://bbs.csdn.net/topics/data;
Return this.
}
Public Builder ok () {
Enclosing the status=HttpCode. OK. GetCode ();
Return this.
}
Public Builder fail () {
Enclosing the status=HttpCode. SERVER_EXCEPTION. GetCode ();
Return this.
}
Public ResponseEntity build () {
Return new ResponseEntity (this);
}
}
}
CodePudding user response:
The above example change@ Getter
Public class ResponseEntity
Private String status;
private String msg;
Private T data;
Private ResponseEntity (Builder Builder) {
Enclosing the status=builder. The status;
This. MSG=builder. MSG;
This. (T) data=https://bbs.csdn.net/topics/builder. Data;
}
Public static class Builder
Private String status;
private String msg;
Private T data;
Public Builder status (String status) {
Enclosing the status=status;
Return this.
}
Public Builder MSG (String MSG) {
this.msg=msg;
Return this.
}
Public Builder data (data) T {
This data=https://bbs.csdn.net/topics/data;
Return this.
}
Public Builder ok () {
Enclosing the status=HttpCode. OK. GetCode ();
Return this.
}
Public Builder fail () {
Enclosing the status=HttpCode. SERVER_EXCEPTION. GetCode ();
Return this.
}
Public ResponseEntity build () {
Return new ResponseEntity (this);
}
}
Public static void main (String [] args) {
ResponseEntity MSG=new ResponseEntity. Builder (). The status (" 1 "). MSG (" MSG "). The build ();
System. The out. Println (MSG);
}
}
CodePudding user response: