A common understanding of object interfaces

Source: Internet
Author: User

Some people write code, write the code according to the computer thinking mode, and write the code to implement the function, but the scalability is poor, while some people write the code, it is written based on the ideas of people looking at the world. The written code looks like such a thing, and it is also very logical. Why? Why is writing code completely different?

I have been reflecting on my own code recently. I used to write the code to complete a function. After I finish writing the code, I am done. But this is not the case recently, recently, I want to ask the question: Can I write code at the functional level? The answer is,The code can be written more flexibly and flexibly.

Today, let's talk about the skill of writing code. We often use the idea of object-oriented programming in some occasions. However, in my real work experience, this is often not the case. Many of them are engaged in process programming in the face of objects.

Let's take a look at some of our current scenarios. Many people often write a user-role-based management system, such as the credit Calculation Method in the member management system, such as A-level members, after purchasing item B, he can get the corresponding points with the points of C, while the B-level member buys the points of product B. At this time, he gets the points of C + 3, then, let me think about how to compile the points writing rules?

Imagine if we use the most common method, we may write this in 80% of the cases?

// Pseudocode if (Level = A) {count (c); // calculate A-level Member points} If (Level = B) {count (+ 3 ): // calculate B-level Member points}

According to the above method, as the level increases, such judgment types will increase. The direct result is that more and more files need to be maintained and the code will become bloated. The final code is directRotten and deteriorated

So what is another way of writing?

// Pseudocode public interface imember () {public double count (double value);} public class amember implements imember () {public double count (double value) {value = value + C; return Value;} public class bmember impelents imember () {public double count (double value) {value = value + C + 3; Return Value ;}}

Then, we only need to generate different object instances based on different login member information, and call the same calculation method. In this way, we only need to add the corresponding level member object when adding classes, so that we can successfully separate the code and place it in different class files, in this way, the latest demand changes can be completed at the minimum change cost.

In fact, what I want to express is that if we want to write programs by using the object-oriented thinking, we can refer to standards. Let's talk about only part of it today, and then take it further.

First of all, you must understand the use of interfaces. This is the most important thing in the face of objects. I don't want to talk about the written descriptions of interfaces any more. There are quite a few descriptions in various reference books, now I mainly want to explain my understanding of this interface and some common questions and answers.

1. In designing coding books, remember to face abstract programming rather than specific implementation programming? Why?

In fact, the answer is very simple, but it may not be that easy to understand. I tried to tell you what happened in the simplest way. In the interface, we often write some methods, right. And these methods are often not implemented, right? The abstract programming in those data actually refersThis unimplemented method Programming. Why? Because the method defined in the interface must be abstract, although it is not displayed that it is abstract, it is true.

In addition, these abstract methods are mapped to our actual development. These methods are a group of objects.Actions simultaneously,But the specific implementation is different.In this case, do you understand exactly what this interface is and what it can be used? Abstract programming for objects is such a truth and meaning.

In fact, you can find a lot of well-designed architecture designs. You will find a bunch of interfaces in them, and then there will be a lot of class structures to implement such interfaces, so that you can have some methods that you can use, so here I recommend that you,In all classes, except for all the methods used in the class itself that are similar to the auxiliary methods, it is best to put all the other methods in each interface for the best implementation, because the maintenance becomes the lowest, minimum modification code cost.

In fact, many people always remind themselves that we cannot write code in the way of the process before writing the code, but once the project starts, these are basically unreliable. Why? Because the subconscious in his mind never thinks about it, where did I come from? So I suggest you think about the following three questions: When you write a class!

1. Is my class A JavaBean? Except the getter and setter methods

2. Is the method in my class implemented from an interface? However, it is recommended that you first think about this problem and decide the source of your method. This will save you a lot of trouble and give a blood lesson.

3. Pause for 60 seconds and then ask yourself the two questions above.

From one thousand to 10 thousand, the interface actually solves a problem, that is, the common problems of many actions. Simply put, A has the shouting skills and B has the shouting skills, however, a will only shout at language A and B will shout at language B. If we use an interface, the specific implementation will be automatically blocked. In this way, polymorphism occurs (that is, when the object instance is a, a starts to shout, and B is still shouting when the object instance is B ).

To put it bluntly, we can understand that an interface is actually a leader. He can only exchange where he can, and never care about how to implement the interface. As long as the result is reached, the class implementing the interface is like a subordinate under the direct jurisdiction of the leader. You only need to follow the general direction of the class and then tell the leader the result. How do you do this? I want to do it myself... the lead will never ask you technical details, because he has his own macro (SHA) Wei (BI) blue (yi) diagram (GE)

A few worths, programming requires skills and thinking, but here we clarify a misunderstanding, that is, there is a saying in the industry that "write more and practice more" you can become a bull? I thank you for reading more code, rather than thinking about it. You will always be a skilled engineer, a coder, rather than a developer. So I advise all the bloggers who read this article to keep in mind that "writing more, practicing more, and becoming a bull" is a prerequisite, it must be completed on the basis of "think more and learn more !! Any statement has a contextual meaning. This is the philosophy of design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.