📜 ⬆️ ⬇️

Why OOP

We continue about OOP .

There is a superficial understanding. I will try to briefly answer the question "why" (realizing the grandeur of the plan).

Let's do this. Consider the question “why OOP”, give the answer in a few words. Express the maximum benefit of all the benefits of the PLO.
')
Do not click "read more" until you answer for yourself.



Of course, no one knows the correct answer. But the two most important benefits are:

1. EFFICIENCY OF UNDERSTANDING BY HUMAN

2. LOCALIZATION OF CODE EFFECTS

1. Simplification of understanding - man. After all, our human language is so arranged. Remember the joke joke?

What is a “chair”? "This is a chair, sitting on it." This is both a behavior and a subject.

Man has become accustomed to designating the _class of objects of similar behavior_ in one word, which defines his behavior.

What is surprising is that this practice has migrated to programming?

After all, it is much easier to talk about the stack than about "this set of data and code that the last object entered first".

2. Localization of influence.

Let's imagine the wrong application development. For example, there is a structure with user data and there is a journal entry structure. When you save to disk, you need to work the same way - treat them as a continuous data block. However, this work has not been done. BEHAVIOR NOT REVEALED. It means that in any place we have duplicated the code for writing to the file, system calls, etc.

We are starting to expand - we want a transactional record. How to be? It is necessary to fasten it _ko to all places where there is a recording on a disk.

Etc. After a while, the extension of the application becomes TOO expensive. We get what is called the "monolithic application." This is bad, everyone seems to understand this.

Attention is a question. And what is the "anti-monolithic" application? Stop, think. What is the ideal architecture?

Everything is simple - it is easy to EXTEND as part of the tasks.

How to achieve this?

The correct answer is this - you need to select the same behavior and program it in one place.

The next question is the same “what” behavior? Component, class, object? And is it important?

It doesn't matter what you call it. What is a DB? Object, class, module, component? IT DOES NOT MATTER.

It is important that we allowed to have our own CONDITION for the implementation of the behavior for managing external data storage.

And they “freed” their code from this behavior.

You probably already guessed what I'm getting at.

To the fact that the principles that I call the "OOP principles" (the separation of behavior and the resolution to have one's own state) naturally arise when solving _any_ more or less complex problems. This is important and I will repeat:

Only separation (specialization) of behavior allows you to create an architecture that easily expands. The separation of behavior means the appearance of its own (local) state and, therefore, some entities that store the state arise. The “locality” of a state means that the effect of the code is limited by the entity.

Let's call entities objects (in the sense of instances of classes), and behavior - a class.

This is the PLO.



The implementation mechanisms remain on the conscience (or dishonesty) of the creators of languages ​​and compilers.

Somewhere there is an automatic destructor, somewhere - no. Somewhere there is private data class, somewhere not. Somewhere there is inheritance, but somewhere it must be constructed. Somewhere there are explicit classes, but somewhere an object can be anything. Somewhere there are interfaces, somewhere not.

This is not OOP. These are the implementation mechanisms.

OOP is the APPLICATION ARCHITECTURE

The main principle of OOP - EXTENDED CLASSES.

DISCOVER (LOCALIZE) BEHAVIOR AND CREATE FOR THIS CLASS.

And further. This is NOT FOR BEGINNERS.

_Use of objects_ is not OOP. If a programmer can call Math.random (), he does not become an OOP programmer.

But the development in the style of the PLO - this is not the basics. Don't start programming.

To identify the behavior - you need to have experience. You need to understand what is "specialization of behavior."

How to be a newbie? Learn well-designed applications. In the area that is interesting.

Source: https://habr.com/ru/post/111162/


All Articles