Summary: Packages and abstractions are important concepts in C , this article trys to make a simple discussion. The package is an important feature of OO. This is no doubt. As for the three major characteristics of oo (the other two are inheritance, polymorphism), I don't want to be here. Excessive entanglement on the problem.
What is package? When the package is a method of solving the problem in the real world, in order to make a simplified problem, a method of research on the subject, an information shielding technique. For example, when you call, we only need to press several buttons. We don't have to understand the lines of the phone, the connection between the network, so that through the package, it makes itself complicated. The problem becomes very simple, easy to operate, and the chance of error is greatly reduced.
The main purpose of the package is to reach the separation of the interface and implementation. By encapsulation, we will hide the implementation details, and we provide the corresponding services to our customers through the interface. Separation of interfaces and implementation is to avoid the key "漪 phenomenon" (漪 现, "," a stone exclaiming thousands of waves ". In the software development process, it will often appear for a purpose, modify a file A, and the result must Modify other files B, C, modify files B, and c will modify the relevant files D, E, F, G, so loop reciprocation, will cause large-scale modifications, eventually spread to the system in waterfall All places. It's like a stone in the middle of the lake, causing fluctuations in the entire lake, "漪 phenomenon" is here).
Separation of interfaces and implementation is a means of software reuse, and its role is mainly reflected in the following four aspects:
1. Reduce the coupling between the internal modules in the system, making the system more flexible.
2. Precise the bug in the software to a smaller module to help the software debugging work.
3. Reduce the complexity of the system and make customers more easily understand and use.
4, the simplicity of separation, more helpful for the reuse of the software.
Abstract is the most simplified interface of the object, and he provides customers with the desired service. Understand the key to the abstraction, the interface, abstraction is an abstraction of the object interface, for a specific object, abstract is a clear interface, for the phone, buttons is his interface (abstraction).
The key to the abstract abstraction is the deep understanding of the research. The better abstraction of the design can separate from the implementation, by providing the necessary information, hiding the implementation details, allowing customers to use objects in a more secure and guest prediction, while reducing complexity, simply interface The way to reduce learning difficulty and improve learning efficiency.
Good abstraction, should make the customer to dispel the idea of "peep" (but some people think about finding a specific implementation), according to abstract programming, make the final customer code simple, safe and stable, in this regard, standard procedures The library and various quasi-standard libraries do the best.
A good abstraction must provide clear, complete, easy to understand, depending on abstract programming, actually relying on these instructions for programming, and he has a big advantage than dependent implementation:
1. Reading instructions is much easier than reading source code, and there is a lot of time, and it is more likely to understand.
2, through the integrity of the description, we can understand a class's integrity by explaining the integrity of the description (for example, the first condition, the rear conditions, applicable ranges, etc.).
3. Through the description, our interface has greater elasticity and scalability (for example, we can use a more effective way to implement the SORT interface).
4. The specified interface must be explained that the interfaces that do not have the corresponding description are defined as errors, making the maintenance of the class easier. A good description should be that the service must be provided, the task that must be completed, not what has been done.
The package and abstraction are inextricable.
The package is provided by providing abstraction, hiding the implementation details, notifying customers that those who can use casually use, those that may change as version updates, such as an interface: void sort (); via interface Explanation, customers can know that this interface is used to sort, what is the specific sorting method, the customer does not need to care, may be in version 1, the use of bubbling, in the upgrade version 2, is rapidly sorted. Customers do not have to care for details, then what methods do not have any impact on the customer's code. For an published interface / abstraction, we cannot change freely, such as providing an Sort interface in version 1, then there must be this interface in the upgraded version 2, because the version is published, we can't know, this The interface is not in use. If we disable the sort interface in version 2, then the customer code that uses version 1 cannot be upgraded. If the upgrade is forcibly upgraded, it will bring the interrupt of customer code, this feature is especially obvious in COM.