C ++ design a dynamic class structure with dynamically adjustable member variables and member variables
This article describes how to use c ++ to Design Abstract Dynamic class structures that can be dynamically adjusted by member variables. First, we will introduce a type of structure used in the project: static class structure.
1. Static Class Structure
Many times, when designing a class structure in project development, we often have a simple and direct inertial thinking: What the raw data looks like, and the class members in the design contain the corresponding member variables, this class is called static class structure. The static class structure has two features.
1.1 feature 1: The class structure abstraction capability is insufficient.
The static class structure is strongly dependent on raw data, and is intended for specific programming rather than abstract programming. Once a field is added or deleted from the original resource, the class structure requires adjustment of the class member variable, which is labor-intensive and manual. For example, video. json of video and video resources has the following content:
List 1: video. json
{"Id": "1009", "name": "Hero Huang Feihong has a dream", "isenabled": 1, person "," hotlevel ": 0," source ": 0, "director": ""}
Static class VideoRecord structure designed for this resource:
Listing 2: class VideoRecord
1 class VideoRecord 2 { 3 public: 4 INT hot; 5 INT hotlevel; 6 INT source; 7 INT isenable; 8 string id; 9 string name;10 string director;11 ......12 }
If the field copyright is added to video. json, the corresponding member variable m_copyright will be added to the videoRecord class. Example:
1.2 Feature 2: lack of generalization ability of class structures
Once data resources are added (for example, chanel. json), or re-channel the resource. json adds a new class structure ChannelRecord or merges the channel. json and video. all fields in json are added to the original VideoRecord class. No matter how you do it, in the face of changing needs, you may feel this intuitive feeling: such code abstraction and generalization capabilities are too weak.
Then, can we design a type structure that can effectively solve all the problems existing in the static class structure, whether it is to add or delete a field or add or delete a resource in the future, don't we have to spend time and effort on this? To this end, you need to spend some time on the design class. below is the turn of dynamic class structure.
2. Dynamic Class Structure
With just a few changes, we can implement dynamic class structures that can be dynamically scaled and adjusted, such as member types and numbers. 1. redesign the class structure and define the pointer m_ppMember pointing to all member variables. You can access all pointers pointing to member variables through m_ppMember.
Second: With m_ppMember's pointer to all member variables, how can we know the number and type of variables? A configuration file is required to specify the number of fields in the resource and the type of each field. In addition, you also need to record the description table. Each resource generates an independent record description table, which is generated by reading the configuration file for dynamic class structure, therefore, the record description table serves as a bridge between the dynamic class structure and the configuration file. Because the Record does not contain the description information such as the member variable type, the serialization and deserialization of the Record object can be completed only by searching the corresponding Record control table with m_type.
Listing 3: config. json
"Video": [{"field": "name", "type": "string" },{ "field": "id", "type ": "string "},
{"Field": "actor", "type": "string"}, // actor {"field": "hot", "type": "int "}, {"field": "hotLevel", "type": "int "}....... // other fields, omitted]
"Channel": [{"field": "name", "type": "string" },{ "field": "id", "type ": "string" },{ "field": "comment", "type": "string" },// comment {"field": "hot", "type ": "int "},....... // other fields, omitted]
Listing 4: recCtrlTable
1 # typedef string FIELD2 # typedef int TYPE
2 # typedef int POS3 class RecCtrlTable4 {5 private: 6 std: map <FIELD, std: pair <TYPE, POS> m_recDes; // The record control table will be based on config. json configuration, Record each field and its type and location in m_recDes for dynamic class Record use 7 ...... 8 };
3. generate dynamic Class Object
The above describes how to design a dynamic class structure. With the dynamic class structure, how can we use this class to generate a class object? For example, use the preceding video. json, with the director, name, and hot fields:
(1) When the program is running, first load config. json to generate the RecCtrlTable Class Object m_recCtrlTable;
(2) Based on the field in m_recDes in m_recCtrlTable, read the feild field corresponding to the video resource and apply for pChar1.. pCharN in memory to store the field value;
(3) Assign the first address of the pointer pChar1. .. pCharN to m_ppMember in the dynamic Record object;
From the above description, we can see that each Record class object only records the resource type m_type and the pointer of the member variable value of this object, and writes and reads the member variable value of the Record object, it depends on the record control table. In this way, although the implementation complexity is increased, a large part of memory is saved.