What abstraction is actually a very mysterious thing. In fact, a lot of code is written around us in a procedural manner. We all know that the three basic principles of object-oriented are encapsulation inheritance and polymorphism. We are often brainwashed by these textbook definitions over and over again. But in fact, in my opinion, what abstraction is...
Abstract is actually a very mysterious thing. In fact, a lot of code is written around us in a procedural manner.
We all know that the three basic principles of object-oriented are encapsulation inheritance and polymorphism. We are often brainwashed by these textbook definitions over and over again.
But in my opinion, no. I think object-oriented programming is ultimately an abstraction, a modeling process. Modeling here is not just about modeling in general terms.
So what is abstraction? In my understanding,Abstract: In a business or a series of businesses, find some methods that we think can constitute a class separately and provide it.. In fact, this sentence is not said, but it is really difficult to explain the abstract. For more information, see my article about abstraction.
Why abstract?
Of course, abstraction is not just as simple as it is. Because of a business flow, you may have 1000 abstract methods.
Good abstraction, I think, must be modular first.
Modularization: it is to do one thing and try to do it well in one place. For example, to calculate the order price, we should do our best to make it ready in one place. Instead of calculating it here, wait for other places to calculate it. (The main benefit is convenience during future maintenance and troubleshooting if any problem occurs)
Of course, the modular result is a good encapsulation, which encapsulates everything in a class. It is easier to cope with expansion.
In fact, the abstraction is very similar to the form of small components. Divide the entire business into small components and then combine them.
Another aspect of good abstraction is the ability to extend abstract things well, which requires experience and feeling.
What is abstract level consistency?
First, in the class dimension, the methods of a class must have abstract consistency.
Abstract consistency: the whole pattern should be done in the same dimension.
For example:
public class OrderComposeService{ public void composeOrder(){ //TODO } public void composeSku(){ //TODO } }
Here we areOrderComposeServiceThe main function is to combine orders,composeSku()This method andcomposeOrder()This method is not at the same abstract level.
In fact, I am still very vague about the abstract consistency of class methods. If anyone has an accurate description, I hope to provide guidance.
Then there is the abstract consistency of the method dimension.
public void composeOrder(B b){ composeSku(); calculateShipping(); createPageage(); }
IncomposeOrderIn this method, the three sub-methods are at the same abstraction level. However, if
public void composeOrder(B b){ List list = new ArrayList(); Map
map = new HashMap
(); for(A a : list ){ map.put(a.getId,a); } composeSku(); calculateShipping(); createPageage(); }
As shown in the following method, an extra section in the middle breaks the abstract consistency of the method. Abstract consistent code reading is just like reading native code. No matter whether it is readable, scalable, or troubleshooting, it is better to abstract consistent code. This is also the reason for less comments, and the Code itself is comments.
Problem
Of course, some problems are that the private method in the method changes a lot. Refer to my previous question [about business hierarchy]
(Http://segmentfault.com/q/1010000002631646 "about business layering ").
Of course, if it is a simple requirement, or the business department is very heavy code, you do not need to consider so much.
What do you think of this?
Reply content: What Abstraction
Abstract is actually a very mysterious thing. In fact, a lot of code is written around us in a procedural manner.
We all know that the three basic principles of object-oriented are encapsulation inheritance and polymorphism. We are often brainwashed by these textbook definitions over and over again.
But in my opinion, no. I think object-oriented programming is ultimately an abstraction, a modeling process. Modeling here is not just about modeling in general terms.
So what is abstraction? In my understanding,Abstract: In a business or a series of businesses, find some methods that we think can constitute a class separately and provide it.. In fact, this sentence is not said, but it is really difficult to explain the abstract. For more information, see my article about abstraction.
Why abstract?
Of course, abstraction is not just as simple as it is. Because of a business flow, you may have 1000 abstract methods.
Good abstraction, I think, must be modular first.
Modularization: it is to do one thing and try to do it well in one place. For example, to calculate the order price, we should do our best to make it ready in one place. Instead of calculating it here, wait for other places to calculate it. (The main benefit is convenience during future maintenance and troubleshooting if any problem occurs)
Of course, the modular result is a good encapsulation, which encapsulates everything in a class. It is easier to cope with expansion.
In fact, the abstraction is very similar to the form of small components. Divide the entire business into small components and then combine them.
Another aspect of good abstraction is the ability to extend abstract things well, which requires experience and feeling.
What is abstract level consistency?
First, in the class dimension, the methods of a class must have abstract consistency.
Abstract consistency: the whole pattern should be done in the same dimension.
For example:
public class OrderComposeService{ public void composeOrder(){ //TODO } public void composeSku(){ //TODO } }
Here we areOrderComposeServiceThe main function is to combine orders,composeSku()This method andcomposeOrder()This method is not at the same abstract level.
In fact, I am still very vague about the abstract consistency of class methods. If anyone has an accurate description, I hope to provide guidance.
Then there is the abstract consistency of the method dimension.
public void composeOrder(B b){ composeSku(); calculateShipping(); createPageage(); }
IncomposeOrderIn this method, the three sub-methods are at the same abstraction level. However, if
public void composeOrder(B b){ List list = new ArrayList(); Map
map = new HashMap
(); for(A a : list ){ map.put(a.getId,a); } composeSku(); calculateShipping(); createPageage(); }
As shown in the following method, an extra section in the middle breaks the abstract consistency of the method. Abstract consistent code reading is just like reading native code. No matter whether it is readable, scalable, or troubleshooting, it is better to abstract consistent code. This is also the reason for less comments, and the Code itself is comments.
Problem
Of course, some problems are that the private method in the method changes a lot. Refer to my previous question [about business hierarchy]
(Http://segmentfault.com/q/1010000002631646 "about business layering ").
Of course, if it is a simple requirement, or the business department is very heavy code, you do not need to consider so much.
What do you think of this?
In fact, I have worked on projects over the years, experienced and experienced many types of designs. For example, I want you to write documents according to the document, and even the method names in each service are standardized, I just don't know how to implement them strictly. I think this is the coding of semi-empty filling. There is a coding that completely fills in the blanks and even has reference documents for calling methods, the purpose of your work is to fill in who calls and who will fill in. This is a complete fill-in coding; the requirement is designed and implemented by yourself. I think this is a full-outsourcing coding, many small companies are like this; another kind of claimed project already exists and has its own architecture. Our goal is to perfect it. The company has a new case to start from scratch, your team has an architecture, pg, and qa. It is normal to perform their respective duties.
Have you found a problem in the examples mentioned above? Apart from strictly limiting what you want to do and what you can only do, in fact, the role you are in team development determines the trend of this framework, the ui only cares about the view, and even they only look at the js css pure ui! Services only care about the interaction between the front-end and dao. Dao only cares about how to deliver the service to the db and the db to the service.
If you say this, you may think that my problem is too serious! The examples I mentioned above cannot contain all of them, but they are only a small part. Just like the design pattern, I once saw someone saying, "What design pattern, many of these so-called design patterns have no significance in other languages. "(I am engaged in java.) He is right about some of them, just as China has been focusing on imperfections since ancient times, what does that mean? "Fifty Avenue, forty-nine days, leaving a glimmer of life, let's go together! I have read more about Xianxia novels!
What I want to say is actually not perfect! People are not perfect, and things are not perfect. With your knowledge, experience, and experience, you can always find out the imperfect side from the seemingly perfect aspects of the past, therefore, we still do not need to struggle to pursue perfection. If perfect, there won't be many design patterns, one is enough, what nosql is there, and what mvc framework is there, mvc is only mainstream, and many others cannot be named (because they may not have a good name at all ).
Well, it's wrong that I haven't answered any questions from the subject until now,The above is just to say a truth, just do what you think is right at this moment, and you can think about things in the future as appropriate, however, you have a length in the future, and you will never think too far.
Let's start to explain the answer to the question.
public void composeOrder(B b){ List list = new ArrayList(); Map map = new HashMap (); for(A a : list ){ map.put(a.getId,a); } composeSku(); calculateShipping(); createPageage(); }
In this Code, you think it is inconsistent with the context. The Post "about business hierarchy" has been specially reviewed. Someone later mentioned using helper, manager, facade is right for the "non-harmonious" code, even if you are ready to use XX, isn't it a name? What we want is an idea! I realized that this section is "not harmonious" and I will make it look "harmonious.
In fact, a lot of code is written around us in a procedural manner.
I agree with this sentence. Before learning oo, the teacher told me that many languages are process, function, program, and method. java is object-oriented, it sounds tall, but actually? It's like watching ccav at home every day is wonderful, but how about going out? There is always a gap between reality and imagination.
When it is actually used, it is always easy to write "tedious" oo code, such as the following section
Class I {N attributes of ox X... Public void is a Superman () {invincible .. Write everything here }}
A method is Superman. Why? When writing, why don't you think about it? At least you have to have a pair of red pants and send them to you for transformation? What Will superman do? Is there an optical class for eye laser? Great power? Okay, this involves at least human styles. It can fly! We can reuse human styles here! Isn't Superman still a girl? But female Superman cannot do anything!
Class, which is the property of {N cattle X... Public void is a Superman () {invincible (worse than "I", something cannot be done !).. Write everything here }}
Ps: I am a male, so I did not write a male Superman on it.
Well, female Superman is miserable. We asked her to do less, but they are all Superman! How can I write two classes? Write one! But what should I do if I use a superman method? So, let's get a subclass that inherits the advantages of male Superman! If this is not the case, reload the Superman method. The following is not to be written, and the multi-state child is coming out soon.
Some people who have been working for many years understand the concept above. In fact, the concept is not clearly recorded, but they all know what is going on and I am not very clear about it, but I just wrote it at will. Even if I see something wrong, I can always see it from one angle!
Isn't that the design? Isn't that a layer? Isn't that what I learned? The oo concept I mentioned at the beginning of java is actually quite the same as the saying "Dao can say, very Dao". It doesn't mean that they are talking about the same thing, but the way they describe it, some things may not be clear, but you know, this is just something that can't be said. I saw an example in segmentfault (a human player playing poker and driving ......) There are not many people who help to write the class or segmentfault, or many people cannot see this problem. I dare say that if 100 people ask them to write, there may be 101 methods, you cannot ask one person to write only one type.
Let's just talk about this. The writing may be a bit messy. The big guy just finished watching the music, haha.
What you are talking about is the concept of structured programming. The basic idea is that the process at each layer is similar to the directory of a book, from the whole book to the chapter to the Section.
However, even though the object orientation has one request for services from another object. But it is slightly different from structured programming. It is more like using different chips to form a logical circuit. Each object may not have a unified relationship, but focuses more on the ownership of knowledge and the responsibility of different objects.
There is a programming paradigm course in the Netease open class. If you have the patience to read it, it should help you understand this problem.