3.3 将实值对象改为引用对象
【1】源代码
1 #include <QString> 2 class Customer 3 { 4 public: 5 Customer(QString name) 6 { 7 m_name = name; 8 } 9 10 QString getName() 11 { 12 return m_name; 13 } 14 15 private: 16 QString m_name; 17 }; 18 19 class Order 20 { 21 public: 22 Order(QString customerName) 23 { 24 m_pCustomer = new Customer(customerName); 25 } 26 27 QString getCustomerName() 28 { 29 return m_pCustomer->getName(); 30 } 31 32 void setCustomer(QString customerName) 33 { 34 m_pCustomer = new Customer(customerName); 35 } 36 37 private: 38 Customer *m_pCustomer; 39 }; 40 41 static int numberOfOrdersFor(QList<Order> orders, QString customer) 42 { 43 int result = 0; 44 QList<Order>::iterator iter = orders.begin(); 45 while (iter != orders.end()) 46 { 47 Order order = (Order)(*iter++); 48 if (order.getCustomerName().compare(customer) == 0) 49 { 50 ++result; 51 } 52 } 53 54 return result; 55 }
到目前为止,客户Customer对象还是值对象value object。
即就算多份订单属于同一客户,但每个订单order对象还是拥有各自的客户Customer对象。
我希望改变这一现状:使得一旦发现同一客户拥有多份不同的订单,那么所有这些订单Order对象只可以共享同一个客户Customer对象。
【2】将实值对象改为引用对象
1 #include <QString> 2 class Customer 3 { 4 public: 5 Customer(QString name) 6 { 7 m_name = name; 8 } 9 10 QString getName() 11 { 12 return m_name; 13 } 14 15 private: 16 QString m_name; 17 }; 18 19 class Order 20 { 21 public: 22 Order(int nID, Customer *pCustomer) 23 { 24 m_nID = nID; 25 m_pCustomer = pCustomer; 26 } 27 28 int getID() 29 { 30 return m_nID; 31 } 32 Customer* getCustomer() 33 { 34 return m_pCustomer; 35 } 36 37 private: 38 int m_nID; 39 Customer *m_pCustomer; // 引用对象 40 }; 41 42 class CustomerList 43 { 44 public: 45 CustomerList() 46 { 47 loadCustomers(); 48 } 49 50 Customer *getByName(QString name) 51 { 52 QList<Customer*>::iterator iter = m_pCustomerList.begin(); 53 while (iter != m_pCustomerList.end()) 54 { 55 if ((*iter)->getName().compare(name) == 0) 56 { 57 return (*iter); 58 } 59 ++iter; 60 } 61 62 m_pCustomerList.append(new Customer(name)); 63 return (m_pCustomerList.back()); 64 } 65 66 private: 67 void loadCustomers() 68 { 69 m_pCustomerList.append(new Customer("Wang")); 70 m_pCustomerList.append(new Customer("Liu")); 71 m_pCustomerList.append(new Customer("Sun")); 72 } 73 74 private: 75 QList<Customer*> m_pCustomerList; 76 }; 77 78 79 int main(int argc, char *argv[]) 80 { 81 QCoreApplication a(argc, argv); 82 83 CustomerList customList; 84 QList<Order*> OrderList; 85 OrderList.append(new Order(1, customList.getByName("Liu"))); 86 OrderList.append(new Order(2, customList.getByName("Wang"))); 87 OrderList.append(new Order(3, customList.getByName("Sun"))); 88 OrderList.append(new Order(4, customList.getByName("Liu"))); 89 OrderList.append(new Order(5, customList.getByName("Wang"))); 90 OrderList.append(new Order(6, customList.getByName("Sun"))); 91 OrderList.append(new Order(7, customList.getByName("Zheng"))); 92 for (int i = 0; i < OrderList.size(); ++i) 93 { 94 qDebug() << "order ID :: " << OrderList[i]->getID() << "Customer :: " << OrderList[i]->getCustomer(); 95 } 96 97 return a.exec(); 98 } 99 100 // run out: 101 /* 102 order ID :: 1 Customer :: 0x206348 103 order ID :: 2 Customer :: 0x206250 104 order ID :: 3 Customer :: 0x206a28 105 order ID :: 4 Customer :: 0x206348 106 order ID :: 5 Customer :: 0x206250 107 order ID :: 6 Customer :: 0x206a28 108 order ID :: 7 Customer :: 0x206da8 109 */
【3】总结
从一个类衍生出许多彼此相等的实例,希望将它们替换为同一个对象。将这个值对象变为引用对象。
Good Good Study, Day Day Up.
顺序 选择 循环 总结