Survival statement: younger brother is a beginner, understand not a place, welcome you pointed out, will modestly improve, but also refused to troll and lead war.
Recently, I met encapsulation at work, and design mode happened to see the strategy mode and decorator mode. They understand the collision together and feel a lot of harvest, so I write it down.
1. Encapsulation in JAVA
The first time I encountered encapsulation, or the back of the test, to tell the truth, I forgot almost. How about you? What is your understanding of encapsulation?
1. Encapsulation is the basis to ensure that software components have excellent modularity. The goal of encapsulation is to achieve “high cohesion and low coupling” of software components and prevent the changing influence brought by program interdependence.
2. Object-oriented encapsulation is to encapsulate the code describing the attributes and behaviors of an object in a “module”, that is, in a class, the attributes are defined by variables and the behaviors are defined by methods, which can directly access the attributes in the same object.
3, as is often the case, just remember to let and access the variables method together, and all member variables in a class definition into private, and only this class methods can access to these member variables, it’s basically realizes the object encapsulation, it is easy to find to be assigned to this class of methods, is one of the object oriented programming is basically. One rule: put methods that operate on the same thing in the same class as related methods, and methods in the same class as the data on which they operate.
2. My understanding
I have not used JAVA in my work, so let’s talk about the understanding of code encapsulation.
One, reduce repeated code, reduce the probability of error.
Realizing code reuse can undoubtedly reduce the time of writing code and improve work efficiency. But can reduce the chances of mistakes, disagree. I’m in favor of less code, less bugs, as long as the logic is as rigorous as possible.
The same logic has only one implementation (code).
I didn’t realize this when I was writing a small project by myself, but I did in my work, where there are many people working together and people change frequently.
【 Case Description 】
The same piece of logic needs to be written in five places. If A writes it in each place once, then the next time B revises it, it may not find five places. B changed one place and found that the processing result was still the same. He went to find it again and found that some results were still the same after changing three places. Then he would certainly greet A’s family. It may not be B. A will modify it after A period of time and may not remember all of it.
So this is where encapsulation comes in. Encapsulate the logic and call it in multiple places so that when someone else needs to change the logic, they only have to change it in one place.
The other advantage of doing this is that you can control the logic with only one entry. Better control of data reversal throughout the project. (To be honest, AS a beginner, I can’t explain clearly.)
— — — — — — — — — — — — — — — –
Do you find that this is the idea of Java encapsulation? At the end of the first paragraph:
“Stick to one rule: put methods that operate on the same thing in the same class as related methods, and methods in the same class as the data on which they operate.”
If you get that, and you also get strategic patterns, it seems that design patterns are not as difficult as we thought.
HeadFirst design pattern
Third, it is conducive to collaborative development
If the implementation details of the logic are encapsulated and the call interface is provided to the outside, the caller can easily implement the logic and avoid repeating the wheel.
conclusion
Make an advertisement, welcome everyone to follow the public account, will share some daily study notes and experience.
I am not a big man, and I have made many mistakes in self-study. I feel that THE basic knowledge of self-study is not solid and I have no project experience. I want to share with my friends who are eager to grow up, but I still want to persuade them to switch to IT.
The last word: Don’t spray if you don’t like it!