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)