Tags

, , ,

One of the most important concepts of the object oriented programming is the abstraction. It’s a concept that has several levels and each one thinks and codes in a different way.

Abstraction is so important that if not well thought, the code may become a disaster, not only the creation and but maintenance too. Knowing abstraction and its levels is essential. Are they:

Abstraction Level 1:

  • All represented objects are objects of real life

abstraction1

  • Advantages
    • It’s really simple.
  • Disadvantages
    • Not so flexible.
    • To understand: If you have to include a toy duck that pecks and eat, but doesn’t fly, it would be necessary to make several adjustments.


Abstraction Level 2:

  • Concepts are also represented as objects

abstraction2

  • Advantages
    • It has good simplicity.
    • It has some flexibility.
  • Disadvantages
    • Only partial flexibility.
    • To understand: If you have to insert several ducks, you would have to implement a lot of times the same behavior in all of them, so having a lot of code duplication.

Abstraction Level 3:

  • Behaviors and concepts are also represented as objects

abstraction3

  • Advantages
    • High level of flexibility.
    • Good code reuse.
  • Disadvantages
    • It has low simplicity. (more complex architecture).
    • It’s hard to map from the beginning, the extensible points of the project.
    • To understand: When creating the Ducks project, you don’t know if tomorrow will have other animals and what characteristics they have in common.

Abstraction Level 4:

  • Behavior, concepts and attributes are also represented as objects (Tiny Types)

abstraction4

  • Advantages
    • High level of flexibility.
    • High code reuse.
  • Disadvantages
    • A lot of frameworks doesn’t support this kind of abstraction. Some does, but needs a lot of configuration.

Currently, there is much discussion (and I agree) that, currently, the type of ideal abstraction is Level 3. However, any level of abstraction has its benefits and the most common is to use Level 2 or Level 3, but keep in mind that even the Level 1 abstraction can be considered in some cases.

Use the right abstraction to your problem, always thinking about the possible extensions of the project, bearing in mind that, depending on how the system was modeled and system size, it’s OK to change your level of abstraction is needed. (It is very common to start with Level 2 of abstraction and then switch to Level 3, for example.)

  • It’s worth pointing out that you can use/merge abstraction levels for certain cases. If you haven’t found your level of abstraction here, it’s likely that it is a variation of one or more levels above.

What is your opinion? Do you agree? Disagree? Leave here in the comments what you think and how do you use the abstraction.

Advertisements