bullshit
Good code is more for later maintenance, even if you don’t need to maintain later, clean and dry code will leave a good impression on the author
And can exert influence in the work team
Eliminate complex expressions with encapsulation
The business scenario
Xiao Jiang wants to go to his classmate’s home to watch TV and do homework after school, so he may come home late
He has to tell his family if they don’t agree, he has to go straight home after school
Family members include grandparents, parents, older brothers and sisters (only if one of them agrees)
Waterfall of code implementation
A condition that requires n combinations of logic or code that doesn't is very uncomfortable to read and you have to sort through the logic and maintain a project that's years old, and there's a lot of code that's long and it's hard for people to sort out but it's much better to encapsulate itif(father.permit() || mather.permit() || grandPa.permit() || grandMa.permit() || brother.permit() || sister.permit()){
// Go to your classmate's home to do your homework
}else{
// Go home after school
}
Copy the code
Encapsulation of code implementation
Family family=Family.build(father,mather,grandPa,grandMa,....) ;if(family.permit()){
// Go to your classmate's home to do your homework
}else{
// Go home after school} in the trunk logic aboveif(father. Permit () | | mather. The permit () | | grandPa. Permit () | | grandMa. The permit () | | brother. The permit () | | sister. The permit ()) to internalize The logic in the family business domain is: If the family agrees.. If the family does not agree..Copy the code
Code implements the encapsulation of counterexample | — — — — — – | against it
if(permit()){// go to the classmate's home to write homework}elsePrivate Boolean permit (Father Father,Mather Mather,GrandPa GrandPa,......) {// return home} private Boolean permit (Father Father,Mather Mather,GrandPa GrandPa,......) {returnfather.permit() || mather.permit() || grandPa.permit() || grandMa.permit() || brother.permit() || sister.permit(); } encapsulate logic directly private 1. Unit tests will cover less than 2. After privatization, the same permit() business will be scattered in every corner of the system. After privatization, the entrance to Permit () is not exclusively maintained and is inconvenientCopy the code
^_^ in small package
Xiao Jiang can eat after he finishes his homework
Completion of the assignment will be judged by the study time >1 hour
Student xiaojiang;
if(xiaojiang.studyTime > 1) {// Finish your homework and get ready for dinner} code is shorter and easier to read but it still requires a mental shift than learning time1Finish your homework hour is equal to -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -class Student{
private String name;
private Integer age;
private Integer studyTime;
private. ; Add methods to complete job decisionspublic Boolean finished(a){
if(this.studyTime > 1) {return Boolean.True;
}
returnBoolean. False; } } Student xiaojiang;if(xiaojiang.finished()){
// Finish your homework and get ready for dinner} Code reading logic is: Xiaojiang whether finish homework yes: eat no: other as for what I finish homework future business become age >18The trunk logic stays the same, just change the business representation of the FINISHED () in the student domainCopy the code
conclusion
Use encapsulation to eliminate long code and improve code readability
The encapsulation of external development on the appropriate business model is responsible encapsulation
Privatization encapsulation is not recommended encapsulation
Code must be private encapsulation, not private no solution, might as well jump out of the current business model, God’s perspective to determine whether the current model is appropriate
Some file business and other confidential to private