Dynamic class structure with dynamically adjustable C + + Design member variables

Source: Internet
Author: User

This article mainly describes how to use C + + Design member variables can be dynamically adjusted abstract dynamic class structure. Let's start by describing a class structure that was previously used in the project: Static class Structure

1. Static class structure

Often, when designing a class structure in project development, we tend to have a simple, straightforward inertial thinking: what the original data looks like, and the class members in the design contain the corresponding member variables, which I call the static class structure. There are two characteristics of static class structure.

1.1 Characteristic one: The abstract ability of class structure is insufficient.

Static class structures are strongly dependent on the original data and are specific to programming rather than abstract programming. Once the original resources are added or deleted, the class structure is adjusted to the class member variables, which is laborious and repetitive, and is a non-technical labor of repetition. For example: Film and television resources Video.json, the contents are as follows:

List 1:video.json

{"ID":"1009","name":"the hero of Wong Fei-Hong has a dream",,"isenabled":1People","Hotlevel": 0,"Source": 0,"Director":"Zhou Xianyan"}

A static class Videorecord structure designed for this resource:

Listing 2: class Videorecord

1 classVideorecord2 {3  Public:4 INT hot;5 INT Hotlevel; 6 INT Source;7 INT isenable;8 stringID;9 stringname;Ten stringDirector; One ...... A}

If the new field Copyright,videorecord class is now added to the Video.json, the corresponding member variable m_copyright will be added. Examples are as follows:

1.2 Characteristics Two: lack of generalization ability of class structure

Once the data resource is added (for example: Chanel.json), either re-add a new class structure Channelrecord for the resource Channel.json, or Merge all the fields in Channel.json and Video.json, add the original Videorecord class Rapporteur Regardless of what you do, you may have the intuitive feeling that you have a very weak code abstraction and generalization capability in the face of constantly changing needs.

So, can you design a class structure that can solve all the problems of static class structure, regardless of whether you add or delete a field later, or add or delete a resource, you don't have to spend time and effort on it? To do this, you need to put a little thought on the design class, and the next round is the dynamic class structure.

2. Dynamic class Structure

With just a few changes, we can implement dynamic class structures that can be dynamically scaled, such as member types and numbers. First: Re-design the class structure, define a pointer to all of the member variables pointer m_ppmember, can access all pointers to member variables through M_ppmember.

Second: With M_ppmember pointers to all member variables, how do you know the number and type of variables? Here you need a configuration file that indicates how many fields are in the resource, and what type each field is. There is also a need to record a description table, the record description table is generated by reading the configuration file for use by the dynamic class structure, so the record description table is a bridge between the dynamic class structure and the configuration file.

  Listing 3: Configuration file 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, omitting ]
 "Channel":                [                    {"Field":"Name","Type":"String"}, {"Field":"Id","Type":"String"}, {"Field":"Comment","Type":"String"},// reviews {"Field": "hot", " type" int "}, < Span style= "COLOR: #800000" > < Span style= "COLOR: #800000" > .... //]   

Listing 4: Record Control Table class recctrltable

1#typedefstringFIELD2#typedefintTYPE
2 #typedef int POS 3 classrecctrltable4 {5 Private:6STD::MAP&LT;FIELD,STD::p air<type,pos>>m_recdes;//Record Control Tableeach field and its type and location are recorded in m_recdes for use by the dynamic class record, according to the Config.json configuration .7 ......8};

 3. Generating dynamic class objects

The above describes how to design a dynamic class structure. With dynamic class structure, how do we use the class to generate a Class object? For example: or use the Video.json above, with the Director,name and hot field segments:

(1) When the program runs, the Config.json is loaded first, and the Recctrltable class object M_recctrltable object is generated.

(2) According to the field in m_recdes in m_recctrltable, read the corresponding feild field of the video resource and request the memory Pchar1-n, and store the value of the field.

(3) Finally assign the first address of these pointers pchar1-n to the M_ppmember in the dynamic Record object.

Dynamic class structure with dynamically adjustable C + + Design member variables

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.