Hello, THIS is Yes.

Object-oriented programming is familiar, but do you have a clear understanding of object orientation after writing so much code?

Take a look at these questions:

  • What exactly is object-oriented programming?

  • How is it different from procedural programming?

  • What is called object – oriented language, process – oriented language?

  • Code written in an object-oriented language is object-oriented, right?

  • Is object-oriented programming really that good?

  • Is object-oriented programming appropriate for complex businesses?

I haven’t really defined exactly what object-oriented programming is.

So let’s say someone asks what exactly is object-oriented programming? What are the benefits?

I really don’t know how to say it or explain it systematically.

In this article I’ll talk about my understanding and try to clarify what object-oriented programming is.

The body of the

From binary commands to assembly language.

From assembly language to procedural language to object-oriented language.

The development of computer language is to facilitate the use of human beings, make it more in line with the way of human thinking.

The idea of a computer is to take the finger and execute it, to go straight to the end, and it doesn’t care what abstraction you have, whatever business modeling you have, it has to turn it into a sequence of instructions for it to execute.

We humans are different. In simple scenes, our thinking is one way, but in complex scenes, we need to do all kinds of classification, so as to clarify the relationship and deal with things well.

It is like a court, divided into the roles of judge, clerk, bailiff, plaintiff, defendant, witness and so on.

With so many people sorted and assigned to their respective roles in the courtroom, a case can be tried efficiently and smoothly.

Back to the computer language, I will not say assembly, process oriented is actually a way of thinking, because the original is to write programs according to the computer’s thinking.

Let me take the example of making coffee with a coffee machine, following a process-oriented process:

  1. Perform the coffee bean method

  2. Perform the water addition method

  3. Implement the coffee making method

  4. Implement coffee drinking method

Very simple and intuitive operation, you may not feel, I again according to the object-oriented thought to analyze the process.

Before performing the coffee making operation, abstract: man and coffee machine (classification), and then perform:

  1. People. Add coffee beans

  2. . Add water

  3. Coffee maker. Brew

  4. People drink coffee

Do you feel something?

Process-oriented, as the name implies, focuses on processes, while object-oriented focuses on objects.

This example shows the difference between the two: process-oriented thinking is very direct, executing step by step, one way to the end.

While object oriented is first abstract, classify things into different classes, divide the responsibilities of each class, expose the actions that each class can perform, and then call the methods of each class according to the logical execution, do not care about the internal logic.

From the example, we can see that the steps of object-oriented programming are not reduced, and the overall execution process is the same, which is to add coffee beans first, add water, make coffee, drink, this logic has not changed.

It simply divides the classes, encapsulating the concrete implementation of each step and spreading it among different classes.

For us programmers, it’s very straightforward: what’s changed is the distribution of the code, the implementation of the coffee making code is encapsulated inside the coffee machine, and the implementation of the coffee drinking code is encapsulated inside the person, rather than being written in a method.

The distribution of code is indeed the most intuitive, but becomes not just a distribution, but a change in thinking.

It’s the transition from computer thinking to human thinking mentioned above.

I think the change is due to the development of software and the increasing complexity of the business.

When people write complex software in process-oriented languages, they need to put some data and functions in different files according to different functions, and gradually people realize that this is not sorting first.

And it seems like business analysis can be matched to the real world, right?

Then people slowly summarize and refine the evolution into object oriented, and then extract the key points according to the characteristics of object oriented: encapsulation, inheritance and polymorphism.

This object-oriented thinking is similar to the analytical thinking that we humans do when facing complex scenarios: categorize and aggregate.

So object oriented programming has become the mainstream programming style, because it conforms to the way humans think.

Procedural programming and object-oriented programming change from thinking like a computer to thinking like a human to write code.

So we know that object oriented programming is actually a step forward, a style of coding that is more like the way people think, and it comes from people’s experience with process oriented programming.

Now that we know where object-oriented programming comes from, we believe that knowing where it comes from will help us understand object-oriented better.

So what exactly is object-oriented programming?

Object Oriented Programming (OOP) is a Programming paradigm or style.

The academic aspect is to organize code with classes or objects as basic units, and to use abstracting: encapsulation, inheritance, and polymorphism as code design guidelines.

This is really object-oriented programming.

In fact, the process of making coffee from above should get the meaning of this.

OOP basically means taking requirements and analyzing them, abstracting them to build business models, and creating classes for each model.

Think about business interactions, define interfaces and control access to interfaces based on those interactions, and encapsulate data and actions related to them.

The subclass inherits the superclass to reuse and extend the code.

When the function is executed, it is called by the parent class. During the actual code running, it will be dynamically bound, and the implementation of the call subclass will achieve the characteristics of polymorphism.

Polymorphism, technically speaking, is the phenomenon of the runtime using the same code to behave differently according to different types of instances.

If there is a new feature to implement, just create a new subclass and the old execution logic does not need to change. This is the “open closed principle”, closed for modification, open for extension.

I think it’s a little bit more intuitive to look at the code, and I remember I used animals as an example in college.

Dog is an Animal. Duck is an Animal.

And then you can vocalize, so you have the voice method.

Public class Animal {public void voice(){system.out.println (" Animal "); }}Copy the code

Then make Dog and Duck inherit Animal voice respectively.

Public class Dog extends Animal {public void voice(){system.out.println (" woof woof ~"); } } public class Duck extends Animal { public void voice(){ System.out.println("gagaga~"); }}Copy the code

And then you can instantiate different objects to get polymorphic.

public class Test{ private Animal animal; public void setAnimal(Animal animal) { this.animal = animal; } public void voice(){ animal.voice(); }}Copy the code

If you want a Dog to bark, you just new Dog and set it. If you want a Duck, you just new Duck and set it.

If you add a new animal you just create a new animal class and set it into it. It’s open and closed.

How is it different from procedural programming?

In fact, from the above two examples of making coffee and animals should feel the difference.

The most important is the difference in thinking, which has also been mentioned above.

The other thing is data and action.

Process oriented programming this programming style is to organize the code with process as the basic unit, process is actually the action, corresponding to the code is the function, process oriented function and data are separated, data is actually member variables.

Object-oriented classes, in which data and action go together, are a significant difference.

What is called object – oriented language, process – oriented language

An object-oriented language is a language that has an existing syntactic mechanism to support classes and objects, such as Java.

And, of course, syntactic mechanisms that support inheritance and polymorphism.

Procedural languages, on the other hand, have no existing syntactic mechanism to support basic units such as classes and objects to organize code.

Of course, it’s not just that you write code in an object-oriented language.

You’re going to have a whole class, and you’re going to have a whole bunch of stuff crammed into it, no sense of categorization, no sense of encapsulation, up to, that’s not object-oriented programming.

Of course, it’s not that you can’t write object-oriented code in a procedural language, but it’s not that convenient to write because of the lack of support for syntax, so you need to use some means, so I won’t expand the details.

So the language is just to better support the programming paradigm, but the important thing is a shift in thinking.

Is object-oriented programming really that good?

Conclusion first: there is no silver bullet in software design, no best, only the right.

As mentioned earlier, object orientation is more in line with the human way of thinking, which is actually an advantage, able to handle complex requirements.

Complex demand relations are intricate, we classification, abstraction, encapsulation can get a standardized module (class).

Large projects require a lot of people to work together, because the division is clear and everyone only has to implement their own modules.

Then assemble them again according to the relationship between modules.

The clear context also makes the development of our thinking is also unusually clear, improve the efficiency of development.

And because of the nature of encapsulation, the interior of a class is highly cohesive, exposing limited access with access control permissions, which prevents the data inside the class from being arbitrarily changed and improves the maintainability of code.

There is also inheritance mentioned earlier, which improves code reusability. Polymorphism implemented by inheritance also conforms to the open and closed principle.

I also read a very graphic explanation (read it a long time ago and forget where it came from) that process oriented is egg fried rice and object oriented is covered rice.

The egg fried rice is mixed together, the covered rice is layered, if you don’t want the scallions, the covered rice just moves the dish on top and you just change the dish without the scallions, the egg fried rice is difficult, you have to cook it again.

In fact, this metaphor reflects the idea of object-oriented maintainability is relatively high, and can be reused, more flexible.

Process-oriented is not easy to maintain and extend.

The metaphor is true, and so is the above statement, but I think there is a prerequisite: in the right context.

Although I have listed many advantages of object-oriented programming above, there are no silver bullets in software design. There are no best ones, only the right ones.

When you make something very simple, such as a simple calculator, it doesn’t make sense to abstract and abstract. It’s best to go straight down the path of process-oriented design.

Just like when we write code in daily life, do we encounter a situation where we need to create a new class for a function, and then there is only one method in the class?

Because in object-oriented thinking, this needs to be abstract.

Then in order to reuse also do inheritance, reserved some interfaces and so on, just thinking about later extension.

It may be that after many years, the project has been blocked and not expanded.

In many parts of the project, hooks like this have been made in vain, and no fish have been hooked.

It would have been better if I had just written it straight, as my new colleagues came in and looked confused.

Some people say that code has to be written this way, for later extensions, in design mode!

Ask yourself, how much of it was used later?

So there were a lot of bulls scolding:

“Object-oriented programming is a terrible idea, and only people in Silicon Valley can do it.” “– Edsger Dijkstra, Turing Award winner

“Sometimes an elegant implementation only needs a function. It’s not a method. Not a class, not a framework. It’s just a method.” — John Carmack, founder of ID Software and father of first-person shooters

“The problem with an object-oriented programming language is that it always comes with all the implicit environment it needs. You want a banana and you get a gorilla with a banana and a whole jungle.” “– Joe Armstrong, Inventor of Erlang

There’s a lot more. I won’t list them.

It’s true that sometimes when you’re writing code it’s obvious that sometimes all you need is a function.

So I’m not a fan of the “don’t ask, ask is object orientation” and the “don’t ask, ask is design pattern” evangelists.

Again:

There is no silver bullet in software design, no best, only the right.

Is object-oriented programming appropriate for complex businesses?

The common saying is that object orientation is suitable for complex scenarios, but this is not entirely true.

At the time I was playing LOL, I was wondering about the release of this skill, and then there might be some special calculation due to the addition of BUF, it seems that there are a lot of changes in each version.

Just like when Yasso came out, the wind was absolutely useless to the stone people, and it was useless to be pulled by scorpions. It wasn’t a lot of judgment.

Every time a new hero is created and a new object is created, the other hero objects have to be changed, because the new hero may have different damage effects for different heroes.

Anyway, I thought it was very complicated, and each change involved a lot of things, so I had a question in my head about how to do this, and for object oriented things, I had to change a lot.

A few days ago, I read invalid S’s answer, which gave me some answers.

The original complex business of object-oriented programming is not really appropriate.

He used the example of WOW. I don’t know if LOL does that, but it doesn’t matter.

He let me know that an object-oriented design in this scenario would not be able to cope with such a wide variety of classes, races, and skills under frequent version iterations.

I’ll take a screenshot and link it to the end of the article.

This situation can be solved by spell/skill database, i.e. tabulation.

So we can see that it is not directly object-oriented in the face of complex scenes, or a case-by-case analysis, object-oriented is not a panacea.

The last

In fact, I’ve seen people say that the essence of object orientation is a mapping of the real world, which is not true.

When we write code, it’s obvious that sometimes we create a class for abstraction and reuse.

And in many cases, the abstract class does not correspond to the reality, but it is built for the needs of a class.

There is also a lot of talk on the Internet about OOP being wrong or OOP being right.

I think it’s very extreme, it’s the same software design no silver bullet, no best, only the right.

One more thing I want to mention about object orientation.

I talked about divide and conquer last year when I wrote Fork/Join.

Object orientation actually tastes like that.

In ancient times, for example, the emperor did not know the details of governance, nor did he need to manage the details. A state as big as this abstracted many affairs and divided them into various types of officials.

Then encapsulate what each type of official needs to do, and let each type of official do its job.

The emperor just needed to take the whole picture and issue different tasks according to the responsibilities of each type of official, not how he carried them out.

As long as the emperor said, let each county carry out what what what, can.

In fact, as long as you call the county magistrate to do things, no matter which county you are, the emperor does not need to care.

Then each magistrate gets the same order, but will have their own governance method, which is actually polymorphic.

That’s really the idea of object orientation.

Ok, said so much I don’t know if I can speak clearly what is called object oriented, if not clear, I hope you forgive me, after all, the ability is limited.

Let me summarize object-oriented programming a little bit:

OOP is actually a programming paradigm or style, a way of organizing code by class or object units, so that the code is high cohesion, low coupling.

OO conforms to the way humans think about complex things, abstracting, modeling, categorizing, categorizing.

A 20W word algorithm brush notes waiting for you to get, from a little bit to a hundred million points, we will see you next. Personal article summary: github.com/yessimida/y… Welcome to star!

Shoulders of giants

www.cnblogs.com/hdu-2010/p/…

www.zhihu.com/question/20…