Home > Back-end >  Java is a good or a no-parameter constructor parameters in a set one.
Java is a good or a no-parameter constructor parameters in a set one.

Time:09-20




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/set

CodePudding 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:

reference 1st floor qybao 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 the method can be more secure protection properties, such as to determine whether effective are allowed to set the attribute


But many, if the variable is going to write a lot of set and get, modify the code to pull screen, how to dazzle broken?

CodePudding user response:

Object attribute is not at the time of creation, all attributes will have value, all parameters of the constructor is not practical

CodePudding user response:

reference 5 floor qq_16774199 reply:
Quote: refer to 1st floor qybao 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 the method can be more secure protection properties, such as to determine whether effective are allowed to set the attribute


But many, if the variable is going to write a lot of set and get, modify the code to pull screen, how to dazzle broken?


I think you can write springbean

CodePudding user response:

Getset normal use

CodePudding user response:

If it is a standard bean get/set and no arguments have
According to the business can use a structure and builder pattern

CodePudding user response:

reference 5 floor qq_16774199 reply:
Quote: refer to 1st floor qybao 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 the method can be more secure protection properties, such as to determine whether effective are allowed to set the attribute


But many, if the variable is going to write a lot of set and get, modify the code to pull screen, how to dazzle broken?
can introduce lombok, and add a @ Data annotation on the class

CodePudding user response:

Must be set and the need to manually set up a structure and parameter too often in the structure with automatic initialization, the rest of the set

CodePudding user response:

Whole structure are seldom write, basic it is get/set way,
And front end and values are now directly with the property name assignment way, don't need you to set one by one,

CodePudding user response:

Simple object callback setter set, complex objects using Buidler set good,
Rarely use constructors with parameters,

CodePudding user response:

Depends, some class, just came out of tectonic attribute uncertainty, wait until later to get data set, this is what attributes cannot be determined without first structure method, wait until later to the set

CodePudding user response:

Can consider to have the cords within five, actually this is not important, the build method may be better.
  • Related