"Refactoring – Improving the design of existing code" reading notes----Replace Array with Object

Source: Internet
Author: User

If you have an array where the elements each represent different things, such as you have a

Qlist<qstring> strlist;

Where Strlist[0] represents the player's name, Strlist[1] represents the home address of the contestant, it is obvious that the meaning of this array is already too much, you need to replace the array with an object, and for each element in the array, a field is represented.

Arrays are a common data structure for organizing data, but they should only be used to "hold a similar set of objects in a certain order." For the example above you can see that an array accommodates different objects, which can cause problems for customers using arrays, because it is difficult to remember that the first element of the array is the name, and the second element is the home address. The object is different, you can use the field name and function name to express such information, you do not need to memorize more need to rely on comments, and the benefits of using objects can let you use the move Method adds to his data-related behavior to make the class itself more attractive.

    • Practice:
    • Create a new class to represent the information that the array has, and first save the original array with the public field.
    • Modify all the clients of the array, and let them use the new class instance instead.
    • Compile, test.
    • Add the value/Set function to the array element one by one, name these access functions according to the purpose of the elements, modify the client code, let them use the Access function to fetch the array elements, and compile the test after each modification.
    • When all the direct access to an array is called to the Access function, the field saved in the new class is changed from public to private.
    • Compile.
    • For each element in the array, create a field of the same type in the new class and modify the access function of the element so that he can use the new field as described above.
    • Each modification of an element is compiled and tested.
    • After all the elements of the array have the corresponding fields, delete the array.

Example:

Qlist<qstring> row;row.append ("livepool"); Row.append (" China "= row[0 = row[1];

This array has two elements, where the first element holds the name, the second element holds the nationality, it is obvious that it will bother the client, so we need to refactor, first we declare a class to do array transfer

class performance{    public:        qlist<QString> m_data;};

First we declare this array as public, rest assured, this is only temporary, later we will set him to private:), now we want to find the place to create and access the array, at the creation point, we will change him to the following code

Performance Row;row.m_data.append ("livepool"); Row.m_data.append ( "  China "  = row.m_data[0= row.m_data[1];

We have completed the first step, has already introduced this new class, don't worry, the refactoring is step by step slowly, can ensure that not error prone. Next we want to add a meaningful set of values and accessor functions to the array element, first we start with the name

class performance{    public:        const        {            return m_ data[0];        }         void setName (const QString &value)        {            m_data[0] =  value;        }};

Then modify the code that uses the row object and let them use these functions instead

Row.setname ("livepool"); row.m_data[1"China" == performance.m_data[1  ];

Next we follow the second element, the final code is

classperformance{ Public: QString Nation ()Const        {            returnm_data[1]; }        voidSetnation (ConstQString &value) {m_data[1] =value; }};row.setname ("Livepool"); Row.setnation (" China"); QString name=Row.name (); QString Nation= Row.nation ();

After all the elements have been processed, we can safely change the field to private.

Private :    Qlist<QString> m_data;

Now that we've done the most important part of refactoring----interface, but we're also going to do a procedure to replace an array within an object, I can create a new type of field in the class for each array element, and then modify the Access function that accesses the array element so that he can access the new field directly, thus completely escaping the dependency of the logarithm group.

class performance{    public:        const        {            return m_name;        }         void setName (const QString &value)        {            = value;        }     Private :        QString m_name;};

After all the elements have been processed, we can remove the array from the performance class.

"Refactoring – Improving the design of existing code" reading notes----Replace Array with Object

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.