Deep into C + + API problem detailed _c language

Source: Internet
Author: User
Tags inheritance
The format of this paper is different from that of other languages. The topic of this article is about C + + API thinking, in fact, not only C + +, for other languages also apply.

The API actually refers to the application programming Interface, the application programming interface. The API is a set of predefined Windows functions that control the appearance and behavior of various parts of Windows. In C + + I think: an API is a collection of classes and methods and fields in a class.
Not all signatures are part of the API, and the Non-public part (private) content is not an API, it does not commit to compatibility. The API is broadly defined: anything that facilitates the assembly of a large system in a "thread-free" state is an API。

Why do you want to develop APIs?
We want to assemble large chunks of building blocks "without threads" into applications, and some build modules that contain shared libraries, frameworks, predefined program architectures, and combinations of these content.

How do I check the quality of the API?
1, comprehensible
2, consistency
3. Visibility
4, simple tasks should have a simple solution
5. Protect investment

API Design Tips:
Do not advertise the fields in the class directly to the outside, but should be placed internally. It is best to allow external users to access the field only through the set, get methods, and never publish the field directly.

Using methods to access fields is better than accessing fields directly. If you follow this advice when writing APIs, you can make a number of changes to the implementation of the method in the next release of the API, such as validating and validating the data, overwriting a method, and so on, and making some big changes. The above changes cannot be made if the finger ends are used directly.

API Design Tips Two:
Create an object by using the factory method instead of using the constructor directly.

Benefits of creating objects using the Factory method:
1. The return value of the factory method is not necessarily an instance of the declaring type, it can be an instance of its subclass. Using factory methods can better embody the advantages of polymorphism in object-oriented language, and the code will be more concise.
2. Each returned object is not necessarily a newly created object and can be cached entirely. By contrast, each call to the constructor creates an instance, and a factory method caches the previously created objects and reuses them, saving memory.
3, on the synchronization control, in the factory method can be created before and after the corresponding code to be unified treatment. Constructors are powerless in this case.

API Design Technique three:
Make all the content immutable, and if you don't consider having subclasses, you should let this class not be inherited.
Highly recommended: do not expose constructors, instead provide factory methods

API Design Tips Four:
Avoid abusing the set method, in which case the set method is superfluous.
Recommendation: Be careful with every unnecessary set method in the API

API Design Tips Five:
expose functionality as much as possible through friends. To make it easier for users to use your APIs, do not expose too many methods to your API and not give users the opportunity to make mistakes.

API Design Tips Six:
avoid exposing deep inheritance. Once you find that the inheritance system is over two levels, be sure to stop and think about it, and figure out: "Do I design APIs or reuse code?" "If the answer is the latter, then revisit it and rigorously design the API, or prepare for subclasses."

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.