1 xmind:
    image.png
    2 code:优化前的代码:
    image.png:

    1. #ifndef DYNAMICARRAY_H
    2. #define DYNAMICARRAY_H
    3. #include "Array.h"
    4. #include "exception.h"
    5. namespace DJLib
    6. {
    7. template <typename T>
    8. class DynamicArray : public Array<T>
    9. {
    10. protected:
    11. int m_length;
    12. public:
    13. DynamicArray(int length)
    14. {
    15. this->m_array = new T[length];
    16. if( this->m_array != NULL)
    17. {
    18. this->m_length = length;
    19. }
    20. else
    21. {
    22. THROW_EXCEPTION(noEnoughMemoryException,"No memory to create DynamicArray object...");
    23. }
    24. }
    25. DynamicArray(const DynamicArray<T>& obj)
    26. {
    27. this->m_array = new T[obj.length];
    28. if( this->m_array != NULL)
    29. {
    30. this->m_length = obj.m_length;
    31. for(int i = 0 ;i < obj.m_length;i++)
    32. {
    33. this->m_array[i] = obj.m_array[i];
    34. }
    35. }
    36. else
    37. {
    38. THROW_EXCEPTION(noEnoughMemoryException,"No memory to create DynamicArray object...");
    39. }
    40. }
    41. DynamicArray<T>& operator = (const DynamicArray<T>& obj)//有重复的逻辑
    42. {
    43. if(this != &obj)
    44. {
    45. T* array = new T[obj.m_length];
    46. if(array != NULL)
    47. {
    48. for(int i = 0; i < obj.m_length; i++)
    49. {
    50. array[i] = obj.m_array[i];
    51. }
    52. T* temp = this->m_array;
    53. this->m_array = array;
    54. this->m_length = obj.m_length;
    55. delete[] temp;
    56. }
    57. else
    58. {
    59. THROW_EXCEPTION(noEnoughMemoryException,"No memory to create DynamicArray object...");
    60. }
    61. }
    62. return *this;
    63. }
    64. int length() const
    65. {
    66. return m_length;
    67. }
    68. void resize(int length)//有重复的逻辑
    69. {
    70. if(length != m_length)
    71. {
    72. T* array = new T[length];
    73. if(array != NULL)
    74. {
    75. int size = (length < m_length) ? length : m_length;
    76. for(int i = 0; i < size; i++)
    77. {
    78. array[i] = this->m_array[i];
    79. }
    80. T* temp = this->m_array;
    81. this->m_array = temp;
    82. this->m_length =length;
    83. delete[] temp;
    84. }
    85. }
    86. else
    87. {
    88. THROW_EXCEPTION(noEnoughMemoryException,"No memory to resize object...");
    89. }
    90. }
    91. ~DynamicArray()
    92. {
    93. delete[] this->m_array;
    94. }
    95. };
    96. }
    97. #endif // DYNAMICARRAY_H

    image.png:

    1. #include <iostream>
    2. #include "DynamicArray.h"
    3. using namespace std;
    4. using namespace DJLib;
    5. int main()
    6. {
    7. DynamicArray<int> s1(5);//s1数组初始长度为5
    8. for(int i = 0; i < s1.length();i++)
    9. {
    10. s1[i] = i*i;
    11. }
    12. for( int i = 0; i < s1.length(); i++)
    13. {
    14. cout << s1[i] << endl;
    15. }
    16. DynamicArray<int> s2(10);
    17. s2 = s1;//赋值
    18. s2.resize(3);
    19. for( int i = 0; i < s1.length(); i++)
    20. {
    21. cout << s2[i] << endl;
    22. }
    23. // s2[4] = 100;//越界了
    24. return 0;
    25. }

    3 问题:
    DynamicArray类中的函数实现存在重复的逻辑,如何进行代码优化?

    code:
    image.png

    1. #ifndef DYNAMICARRAY_H
    2. #define DYNAMICARRAY_H
    3. #include "Array.h"
    4. #include "exception.h"
    5. namespace DJLib
    6. {
    7. template <typename T>
    8. class DynamicArray : public Array<T>
    9. {
    10. protected:
    11. int m_length;
    12. T* copy(T* array, int len, int newLen)
    13. {
    14. T* ret = new T[newLen];
    15. if(ret != NULL)
    16. {
    17. int size = (len < newLen) ? len : newLen;
    18. for(int i = 0; i < size; i++)
    19. {
    20. ret[i] = array[i];
    21. }
    22. }
    23. return ret;
    24. }
    25. void update(T* array, int length)
    26. {
    27. if(array != NULL)
    28. {
    29. T* temp = this->m_array;
    30. this->m_array = array;
    31. this->m_length = length;
    32. delete[] temp;
    33. }
    34. else
    35. {
    36. THROW_EXCEPTION(noEnoughMemoryException,"No memory to update DynamicArray object...");
    37. }
    38. }
    39. void init(T* array, int length)
    40. {
    41. if(array != NULL)
    42. {
    43. this->m_array = array;
    44. this->m_length = length;
    45. }
    46. else
    47. {
    48. THROW_EXCEPTION(noEnoughMemoryException,"No memory to create DynamicArray object...");
    49. }
    50. }
    51. public:
    52. DynamicArray(int length)
    53. {
    54. init(new T[length], length);
    55. }
    56. DynamicArray(const DynamicArray<T>& obj)
    57. {
    58. init(copy(obj.m_array, obj.m_length,obj.m_length),obj.m_length);
    59. }
    60. DynamicArray<T>& operator = (const DynamicArray<T>& obj)//有重复的逻辑
    61. {
    62. if(this != &obj)
    63. {
    64. update(copy(obj.m_array,obj.m_length,obj.m_length),obj.m_length);
    65. }
    66. return *this;
    67. }
    68. int length() const
    69. {
    70. return m_length;
    71. }
    72. void resize(int length)//有重复的逻辑
    73. {
    74. if(length != m_length)
    75. {
    76. update(copy(this->m_array, m_length, length), length);
    77. }
    78. else
    79. {
    80. THROW_EXCEPTION(noEnoughMemoryException,"No memory to resize object...");
    81. }
    82. }
    83. ~DynamicArray()
    84. {
    85. delete[] this->m_array;
    86. }
    87. };
    88. }
    89. #endif // DYNAMICARRAY_H