What is abstraction? Are you sure you want to put the horse upside down?

Source: Internet
Author: User
1. What is abstraction?

What do you think of when it comes to abstraction? Are these?
  1. Abstract is the basis of object-oriented. With abstraction, there will be three main features of object-oriented: inheritance, encapsulation, and polymorphism.

  2. The relationship between layers must be dependent on abstraction. The upper layer must be dependent on abstraction, and the lower layer must also rely on abstraction.

  3. In a word, programming is dependent on abstraction.

Let's talk about this. Let's look back at this. What is it?

It is not abstract. It is an embodiment of abstraction. That is to say, it is the result of abstraction. advantages and benefits of abstraction. As a programmer, what we want is the results of abstraction. But a more important task is how to make abstraction "? Extract the abstract and use code to reflect it. For programmers, only implementing their ideas on code is programming and quality programming.
Ii. Why Abstraction


So what is abstraction?

  1. Abstraction with the same

  2. Different fields need to be abstracted

Why abstract?
Abstract: Easy communication for changes.

These two problems are often inseparable. Abstract with no purpose is meaningless work. So here is my opinion.
First, let's start with the first point: abstract when there is the same. Several classes have the same features: Methods and attributes, so you can use abstraction.
1. Few changes. If an abstract parent class is involved in such an overall modification, you only need to modify it in the parent class and inherit the subclass. Saving the trouble of multiple places, the most feared is that the change has not been completed.
2. unified interfaces and multiple options. If there is abstraction, it means that sub-classes can have multiple implementations. polymorphism is the perfect interpretation of the magic of abstraction at this time. It is unified externally, but you can select different "subclass" to achieve different effects.
3. scalability is extremely convenient. The current implementation of classes cannot meet my needs. I only need to add a subclass that inherits the abstract and define the new implementation to achieve the goal, which is consistent with the "Open and Close principle.

Second: different fields need to be abstracted-decoupling closely adjacent relationships

In general, no matter what brand of USB data cable, you can connect to the USB interface of any brand of computer.
The interface here is a set of abstract specifications. As long as different "fields" define the "interfaces" to be accessed, various implementations can be implemented according to the interface conventions. In programming, a system is divided into "N" layers for the sake of coding simplicity, system coordination, and cooperative development. The purpose of layering is to decouple, the two classes to be directly connected are indirectly linked to programming through a set of conventions. Develop your own services in compliance with the conventions. Does not affect each other. If there is no interface, the direct relationship will look like this:

1. Each line needs to be taken from the beginning to the end. If one line is not good, it cannot run;
2. A method changes, especially the underlying method. All classes that call this method need to change.
3. demand changes require that the execution process of the previous classes be changed. For example, if there are multiple discounts in the mall, only classes can be added. When necessary, the class that is called can be changed temporarily, for published systems, this is not a solution.
(Here, you can understand the field as a layer. The concept of layer can also be big or small, with no strict restrictions. People with code experience can divide their own layers based on experience)

From the origin of abstraction, we can see that abstraction occurs for "communication ". If this class is always in the system, it will not be extended, inherited, or changed, then there is no need for abstraction, because it is "unique ". Without change, communication is unaffected. To change, communication must be abstract.

Iii. Abstract embodiment
1. abstract to a base class.Well-known forms. Extract abstract members from several similar classes to form their base classes.

A base class can also be divided into abstract classes and interfaces. The difference between an abstract class and an interface is that the base class abstracts attributes and methods and focuses on the solution of "code duplication; an interface is an abstraction of methods to avoid repeated methods ".

2,Merge similar items without adding parent classes.

This approach is suddenly understood when I recently learned the design model. The transformation from the factory model to the abstract factory model is like this. Let's look at the analysis.

Factory mode: only one product is created: button. Class diagram: create another product: text. The class diagram is as follows: Observe the two class diagrams in the same factory mode, but only one product can be implemented at a time. If two products are implemented, the two diagrams must be combined, then it becomes a "big thing. Let's merge similar objects.
? 1. merge two factories with the method + 1. merge two unixfactroy with the method + 1. merge two windowfactories with the method + 1. They can be merged because they are essentially the same.

? 2. However, the specific buttons, text abstract classes, and specific implementation classes cannot be merged. They are essentially different.

? View the merging result:
No parent class is added and a third party is added.
This is the simplest and commonly used abstraction. It is estimated that some people do not regard it as abstraction. Put the same method used in multiple classes into the third class as a public method. This is often used. It does not establish inheritance or implementation relationships with other classes and is referenced as needed. Of course, in some cases, this third class may be abstracted as an interface for treatment. The specific content is not discussed, and there are too many cases.

Summary: The abstraction comes from the individual and can only be abstracted when there is more. The existing subclass, abstracted, has the base class.
When analyzing the design pattern, start with simple, draw and draw, and have the parent class, inherit, and understand the abstract existence; write code is also, first write, it has an abstract class and an interface.
The above is the abstract thinking these days. Thank you for your correction. Zookeeper

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.