Discussion on design principles of C + + classes and interfaces

Source: Internet
Author: User
Tags inheritance

The main purpose of my article is to introduce the principles of high quality design for a subset of classes and interfaces. These guidelines should not only ensure that the design and implementation of the class or interface itself has high-quality code, but more importantly in the industrial field should be as much as possible to update and maintain the code does not affect the customer's activities, mainly to maintain the binary code compatibility (binary compatibility) and source code compatibility ( source compatibility). I hope these guidelines will help friends who have just entered the industry to adapt to a higher standard of programming as soon as possible to upgrade their design capabilities.

The design of C + + class is discussed in this paper.

Always mention

Object-oriented programming is very helpful for producing high quality, easily maintainable code. The concept of object-oriented programming is built on three basic features: encapsulation, inheritance, polymorphism. In C + +, class is the core and concrete form of object-oriented programming concepts. Class embodies "The encapsulation" through the private member, manifests "the inheritance" through the direct inheritance or the combination, through the virtual function and the dynamic binding (dynamics binding) manifests "the polymorphism". Class design quality directly determines the overall quality of the system.

From the overall functional level to talk about class design, there are three principles:

• Single functional principle (responsibility principle)

A class should provide only a single service in its entirety. If a class provides a variety of services, it should be split, whereas if a single, conceptually functional function is handled by several classes, these classes should be merged.

• Open/Closed principles (Open/close principle)

A design and implementation of a good class, should be open to the expansion of the action, and the modification of the action closed. In other words, this class should be allowed to be expanded, but it is not allowed to be modified. If functional expansion is required, it should generally be done by adding new classes instead of modifying the code of the original class. Adding a new class can be done not only by direct inheritance, but also by composition.

• The principle of minimum surprise (least surprise principle)

Related Article

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.