Why does object orientation suck?

It's the weekend again, and the "big three" of programming languages ​​Java, Lisp and C are meeting at the Hello World Cafe.

The waiter brought a piece of today's newspaper while delivering the coffee. The three of them exchanged a few words, opened the newspaper in C language, and suddenly their eyes lit up: "The title of this article is well written, "Why Object-Oriented Terrible!""

(This is an old article written by the famous Joe Armstrong, the father of Erlang.)

Java was taken aback, someone scolded object-oriented like that? !

He quickly grabbed it, looked at it for a while, and said: "Although I have great respect for the deceased Mr. Joe Armstrong, I don't agree with his point of view. You see he said 'data structures and functions should not be tied together . ! '"

C language said: "What he said is very reasonable. A function implements an algorithm, just like a black box. As long as you understand its input and output, you can understand its function, and the data structure is simply a statement. ’, why tie them together?”

"No, no, it's better to tie them together! Let me give you an example, a stack, if you treat it as a complete object, it will be much more convenient to use."

Stack s = new Stack();

s.push(100);

s.push(200);

s.pop();

The C language is not to be outdone: "It's good to separate data structures and functions! For example, I can create a data structure called Stack, and then write several functions that operate on this data structure."

push(stack, 100);

push(stack, 200);

pop(stack);

Java said dismissively: "Look at how ugly you are in this way."

The C language does not budge: "The essence is the same, you are of(), I am f(o), what's the difference?"

Lisp also interjected: "And my (fo)"

2

Java was speechless, thinking that these two guys were just messing around. Suddenly, his heart moved: How did I forget about polymorphism.

Java said: "The essence is different. You need to know that of() can generate polymorphic behavior, which brings huge benefits. Let me give you an example. You have a piece of business logic that needs to be calculated. The results are recorded in a file, and possibly elsewhere in the future, your design may be like this."

640?wx_fmt=png640

C language: "Shouldn't it be? One function calls another function?"

Java said: "There is a dependency problem here, that is, businessLogic() not only depends on writeToFile() at runtime, but also at the source code level or at compile time."

C language: "Isn't this normal!"

Java also said: "It's not the same. Writing files is a low-level implementation detail, not a high-level strategy. If users don't want to save the calculation results in files, but want to send them by email, then your businessLogic must also be modified. right?"

640

640?wx_fmt=png

C language: "That's for sure! Function calls, if one is changed, the other must also be changed."

"This is the problem. Compile-time/source code dependencies make it impossible for us to divide the system into independent components, which are independently developed and deployed independently. A change in one will affect the other."

The C language felt reasonable, and he said, "Then what should I do?"

Java said: "Look at my design after using polymorphism. My business logic only depends on the interface Writer at compile time , not on the specific implementation of FileWriter and MailWriter."

640

640?wx_fmt=png

"You mean that as long as the interface Writer does not change, the underlying concrete implementations such as FileWriter and MailWriter can be changed and replaced at will, just like plug-ins, right?" C language said.

"Yes, the compile time/source code does not depend, but the runtime depends. This is the benefit of delayed binding. Now you understand the essential difference between of() and f(o)."

Lisp interrupted without losing the opportunity: "Your interface has only one function, which is write(). What interface do you use? Take off your pants and fart, it's unnecessary. I just need to pass different functions to it."

Java smiled and said: "Don't be ridiculous, this is a simple example. Whether it is using an interface or passing a function, it is all delayed binding. The key point is to find the stable thing (Writer), which is abstraction. You find Without this stable thing, if you can’t make abstractions, your system can’t be divided into components that can be developed independently and changed independently.”

The C language still wants to fight back, but it has not been able to find a breakthrough.

Lisp said: "Don't listen to where Java is fooling around, brother C, you can also implement delayed binding at runtime. This is not a patent of Java. Have you forgotten the virtual function table?"

The C language slapped the thigh: "Yeah, I forgot that when I went home during the Spring Festival that year, Linus once told me that virtual function tables and function pointers are the key to polymorphism. For example, Unix/Linux treats devices as files. , there are standard methods such as open, read, etc. For different devices, the corresponding methods can be called, how is that achieved? It is also delayed binding through the virtual function table!"

(Old Liu, a code farmer turned himself over, Note: The details of the object-oriented implementation of C language can be moved to "

The C language is happy: "Haha, Java brother, it seems that we are still the same in essence, polymorphism is just an application of function pointers!"

Java said: "So the key to programming is not whether an object-oriented language is used, do you agree with this?"

C language nods, the key point of programming is to find and abstract a stable interface, and program for this interface, so that each module can be changed independently.

3

"Easier said than done, here is an example, can you give me an object-oriented design?" Lisp threw a question.

Animals can be divided into carnivores, herbivores, aquatic animals, and terrestrial animals. How to express them in classes?

Java said: This is not easy, look at how obvious this noun is, it can be turned into a class, and it is enough to let them all inherit animals.

640

640?wx_fmt=png

Seeing that Java has fallen into the trap, Lisp smiled slyly: "Then some animals are both terrestrial animals and carnivorous animals, how to express it?"

"Then I'll add a terrestrial carnivore." Although he thought it was inappropriate, Java still said it.

640

640?wx_fmt=png

"Then what if there is another aquatic carnivore? Or a new concept 'mammal', how to deal with it?"

"Hahaha, I understand. As the demand increases, not only will classes explode, but also weird classes may appear. This is indeed a big problem with object-oriented programming!" said C language.

Java bowed his head and pondered for a while, and suddenly, the sentence came to mind: Use combination first instead of inheritance.

How to use combination? The way of looking at the problem must be changed, yes, it should be like this:

640

640?wx_fmt=png

Java said proudly: "Look at this picture, animals have multiple characteristics, such as 'eating', 'moving', and 'breeding mode' can be added in the future. Each feature is an interface, and the interface is stable. Animals This concept can be combined through these interface features ."

Lisp nodded appreciatively, and C language cast admiring glances at Java. This guy often does object-oriented design, and he still has two brushes. He isolates changes through features, and each feature can be combined. , replace it at will like a plug-in, well, this is the real essence of object-oriented.

It was late at night, and finally Java made a summary, and everyone dispersed.

"Programming is to discover changes and isolate them. You can use any language. Object-oriented languages ​​​​have the convenience of using polymorphism directly. Don't just diss it in the future."

References:

http://www.cs.otago.ac.nz/staffpriv/ok/Joe-Hates-OO.htm

Agile Software Development: Principles, Patterns, and Practice

"Clean Architecture"

"Object-Oriented Development Reference Manual"

About the author:

, More than 15 years of experience in software development, the author of the best-selling book "", a former IBM architect, has led the design and development of multiple enterprise application architectures; has insight into the essence of technology, and is good at explaining complex technologies with stories.

Wonderful review of the past

640?wx_fmt=jpeg

Guess you like

Origin blog.csdn.net/coderising/article/details/101444370