Started reading a book about patterns. It constantly contains sentences like
encapsulate what changes
What does it mean to encapsulate? I understand the word
encapsulation as hiding information about the implementation of some parts of the code to increase the level of abstraction. But what it means to encapsulate is not completely clear … The thought comes to mind that this is a process of hiding implementation data. But my view does not always match the context from the book …
Answer 1, authority 100%
There are two similar concepts in software development – encapsulation and information hiding. Someone thinks that these are synonyms, some do not, but this is not so important.
A bit of history: David Parnas in the 70th year in the article “On the Criteria To Be Used in Decomposing Systems into Modules” first introduced the concept of information hiding as a key design tool. This principle sounded like this: the decomposition of the system into modules should not be based on the analysis of block diagrams or execution flows. Instead, each module should hide some design decision inside, providing the minimum amount of information about it to its customers.
Here’s a small example.
Let’s say you are developing an externalprise application that does something important. Any normal application usually requires some configuration: well, parameters for connecting to a database or server, and other valuable information. And now, a seasoned artichector (*) together with an equally seasoned client are asked to read the configuration from the configuration file.
(*) this is not a typo, please don’t correct it!
In the process of talking with them, you understand that no one really knows why you need to read the configuration from the file, what format it should have and what exactly should be stored there.
Now you are faced with a choice: you can spread the configuration information evenly throughout your application. Each component that needs some parameters will go into app config by itself, pull out the necessary data from there, parse xml or json and will be ready to serve. On the other hand, it is clear that the decision about exactly where the configuration is stored and in what format may change in the future. Therefore, a more sane solution would be to hide the configuration location and format information in a single module, for example using the
ConfigurationProvider classes. In this case, when (yes, exactly, “when”, not “if”) the requirements change, then only the implementation of the
ConfigurationProvider class will change, and all other users of this class or configuration will remain unchanged. Likewise, changing the format will only change the parsing process, not the configuration consumers.
This example seems contrived, but it is not! We encounter variability in requirements quite often, but unfortunately we use one of two approaches:
• We completely ignore the possibility of changing requirements and do everything head-on or
• We create a super complex solution with a dozen levels of indirection that should withstand changes in requirements in any direction without changing the code at all.
The smarter approach is somewhere in the middle. Every time I start developing a feature, I think how many pieces in the code will have to change if the requirements or implementation details change significantly. At the same time, I’m not trying to reduce the number of changes to 1 (well, like, if we follow the SRP, then there should be only one place, in case of a change in requirements). I try to keep these places small and keep the changes simple.
Actually, this is the essence of information hiding and its younger sister – encapsulation.
Answer 2, authority 42%
This means that you reduce coupling with the rest of the code, and by changing how the surrounding code works, the encapsulated scope remains operational.
By transferring the code to a separate class, you encapsulate it.
By moving the code into a separate method or function, you encapsulate it too.
All implementation is hiding behind the interfaces and works through them, turning to third-party people in a “black box”, which they cannot pick (if the language allows), or can pick, but they understand what they do at their own peril and risk.
Answer 3, Authority 33%
encapsulate – it means to hide. The idea of encapsulation is that you hide the implementation of your classes from users (and programmers), providing them with the interfaces of your classes to work. It also implies data hiding using access identifiers (Public, Private, Protected). For example, you are a programmer and give you access to the library. Namely, you connect a header file, with access to the interfaces of some classes, and manipulate with objects of this class, through the methods of this class. This eliminates the possibility of damage to the working code. Something like this …
Answer 4, Authority 33%
There is a
method>link (); .
Sales can be very variable:
mining rye white bread Praise? Brokek crust ...
It is only the mining of the base. Incomplete version.
Therefore, to hide (encapsulating, as young people say now) it costs at least two reasons:
- Do not scare the end user with the interface with such a variety, for it is frightening it and not need to know (simplicity – the key to use).
- to accidentally and / or intentionally not damage the logic of the sandwich plant.