Encapsulate it

    The real purpose of encapsulation is to gather in one place the knowledge related to the device of a certain entity, the rules of handling and operations with it. Encapsulation appeared much earlier than it is thought. Modules in C programs are encapsulation. Assembly language routines are encapsulation.

    The opposite of encapsulation is the spreading of knowledge about the functioning of something throughout the program.

    When implementing behavior, we sometimes choose from several equivalent options. In the future, the weight of one of the rejected options may increase under the influence of changed circumstances, the option chosen and implemented earlier will become unsuccessful. Change the decision will be much easier if absolutely all the details of the implementation of the selected option are concentrated in one place.

    Example: working with money. It is no secret that in many e-commerce systems, monetary values ​​are implemented as floating-point numbers. I think all of us know that with a simple addition of two "integers" numbers, represented as variables with a floating point, "a slightly non-integer number" can be formed. Therefore, with such an implementation, here and there it is necessary to insert a call to the rounding function. This is the blurring of knowledge about the device of the entity throughout the program. In this case, encapsulation is to collect (hide) in one place the knowledge that money is represented as a floating point value, and that it constantly has to be rounded off during the most innocent operations. Hide so that when using the essence of "money" it is not even talked about rounding.

    Also popular now: