c++ object oriented class design

  • 2020-05-26 09:51:38
  • OfStack

Class design is to use just the right information to fully express a clear concept of responsibility. Just the right means no more and no less. It's redundant, it's redundant, it's redundant, it allows for a little bit of repetition in the special case of course, but there has to be a good reason for that. Redundancy often means that it contains too much information, and the concept expression is not accurate enough. For example, goto, pointer, often inherits these goods, and its use should be strictly limited because of its excessive connotation. Like, more effective c++ says, the member function of class should be kept minimized in its entirety. But the starting point here is the complete minimization of member data.

The benefit of minimization is that you can maintain the maximum independence of the concept, which means that you can implement the concept at the minimum cost, which means that you need less code at the application level, non-intrusive, right? c++11 noexcept instead of throw(), c++11 noexcept instead of throw(), c++11 noexcept instead of throw(), c++11 noexcept instead of throw() For example, if you do not require a read-only string to end at 0, you can treat any part of a read-only string as a read-only string. The external functionality of a class is important, but so is what a class can't do.

The first is to have a clear concept and the most basic operation that the concept supports, and then to organize the data on this basis, so as to minimize the member data. Of course, the concept does not come out of the head, because there are too many times in the code of that kind of relevant data, so it is necessary to package these data, abstract into a concept. For example, if you see the iterator that the stl algorithm function parameter starts and ends everywhere, it is necessary to put the start and end at 1. For example, the appearance of string_view, assuming its character storage type is char, string_view is the meaning of continuous char memory block, which can be expressed as follows

struct string_view
     const char* textBegin;
     size_t length; // or  const char* textEnd

The point here is that the two member fields in string_view are missing 1, but you don't have to add anything else. Then, in the two data to realize the member functions of 1 series, here, both member function and member fields, the tangle of have a little chicken eggs raw chicken, because the set of member functions necessary (original concept elaboration), a member function determines the member fields, said the member fields decided, after this, in turn, to verify the necessity of a member function. However, the design of member functions must follow the principle of minimum completion. More specifically, if a member function can be implemented by other member functions, it means that the function should be expelled from the class and exist as a global function. Of course, this is not a rigid dogma, some very special functions, you can also be a member function, because the use of member functions, really convenient.

It may be confusing to feel that all member functions can actually be global functions. In other words, we can do a pair of set and get functions for every member field, then all other member functions can be in the form of global functions, and it is easy to follow the principle of minimum completion. This, of course, is a gross laziness, a refusal to think. Instead, open up all the member fields, which would fall into the c language. So the theory of law is, a function, it is assumed that is a global function, if its implementation must have access to the member fields, not by calling the class member functions (1 and not get set) to achieve a goal, or, also can use other functions to complete the task force, but very troublesome, time space or have to pay, that means this function should be the class member function.

The design of the class is the essential member field and the essential member function, which realize the full expression of the original concept of the class, and nothing else. A class if bad writing, often means that the function of this class is not only 1, incomplete or its concept, at this time, don't have to rush the abstract, if a class is necessary, so in the writing of the code, the class of abstract concepts to 1 again repeated, ape understanding of it is becoming more and more clear, so, just to make it. All non-demand-driven, non-code-driven, patting on the head, making classes for granted, is the art of making abstractions out of real life, and eventually it will be obsolete.

Class is designed to fully express a clear concept with the necessary data. And inheritance, it is the concept of the class, refinement, classification, such as biological out animals, plants under the two subclasses, is divided the creature into these two kinds of animals, plants, inheritance and the classification of the daily life not too 1 sample, the classification of the inheritance way is open, according to the need, can add a new subcategory at any time. The whole system of classes is a strict single root tree, any class can only have one root class. From any class, there can be only one path back to the root class at the beginning. java, C# is Object. All classes are derived from Object, which is a big tree. With a single root, everything is an object, which is convenient, at least in the sense that it directly supports c++ std's garbage, any, at the language level. And because java and C# have perfect reflective information, you can do things at the dynamic language level without static typing, while c and c++ void* have all the dynamic typing information in the ape's brain. There are a lot of dynamic languages on the java platform, and they're all pretty good.

In contrast to many languages, c++ is a freak and an anomaly. It has its own design philosophy. It is multi-root, and it cannot and does not need to be a single root. All the classes of c++ make up a forest in which the trees grow from the ground. But no matter how to say it is good, can only single inheritance is allowed, do not have multiple inheritance, this is the bottom line, software can't break it (of course, the occasion of exotic curiosity-a solution looking, you don't have to obey the commandments, multiple inheritance thousand is not, but indispensable, because it can play a lot of things, and are practical necessary). At the very least, the single root out of the memory layout is intuitive and predictable, 1 to some extent across the compiler, only a good memory layout, can be expected to be good binary multiplexing. In addition, the parent class has no knowledge of subclass 1. Do not refer to subclass 1 at all, and keep this information flowing in one direction.

In this hierarchical system of single root system, the whole society is lifeless and has no social vitality. Clearly, that little bit of commonality can only be Shared between categories that belong to the same parent. If there is no interface trouble, what would be a tragedy, the best example, mfc, is really bad, don't use interface, incredibly can do rigorous can meet the needs of most gui framework, there is no interface, doesn't mean it doesn't need to, because mfc opened the back door, using the worse thing - message is sent, even so, mfc base class in some places there is dependence to subclass, it is to let a person speechless.

Under c++, the design of the class is definitely not a joke, 1 must be clear about what you want, the abstract concept will not become garbage. Large and complete class, far less than a few small and specific subclass. java, C # under the kind of development is very convenient, but the size is too big, 1 took things to you, strong selling strong buy, anyway as long as the class definition, 1 corresponding will appear the 1 big tuo perfect reflection information, while inside the object also contains 1 some irrelevant member fields, and the access object, and all of them are indirect references to visit, though, computer performance surplus now, these are harmless. c++ gives developers the biggest choice, while the apes of c++ basically have an excess of intelligence, and they know the cost and purpose behind each choice. Therefore, although there is mental baggage affecting the development efficiency, there is no guilt of performance baggage in the heart. Personally, or like the language c + + the highest degree of freedom, sometimes, for memory the most meticulous control, you can get more streamlined design, performance has nothing to do here, say, in c + +, as long as the memory layout 1, even different types of objects, by casting series 1, and then do strange things, like COM inside, in order to aggregate the reuse, a class, can with an interface of two sets of implementation. This convenience is not supported in other strongly typed languages.

In a sense, the language mechanism provided by c++ on object orientation is to easily generate various memory layouts and the operations that can be supported on this memory layout. Virtual functions are used to generate 1 heap member function pointer, inheritance is used to conveniently generate 1 blob member field... . Therefore, the object-oriented design of c++ is memory layout oriented, and multiple inheritance, virtual inheritance, templates and other such things can easily lead to memory layout out of control, but if used properly, it is also a marvel, creating a miracle that other languages do not have. Indeed, when it comes to dynamic performance art, any language is a kindergarten pupil in front of the adult c++.

In order to elicit the interface, this seat spent great efforts to do science. This can't be helped, because classes are the foundation, but the essence of static object orientation is all on the interface. Only by clarifying the functional responsibilities of the class can we understand the necessity of the interface and its diversity. So, can you just have interfaces and no classes. Yes, just like com, but at the cost of all sorts of inconveniences. In this world, there has never been a panacea. To be able to do anything means to be able to do nothing well.

Related articles: