《重构:改善既有代码的设计》 重新组织数据 之 3

版权声明:请注明转发出处 https://blog.csdn.net/mafucun1988/article/details/89458384

《重构:改善既有代码的设计》中提到过很多重构方法,关于重新组织数据的方法有8种。本文介绍:
将值对象改为引用对象 change value to reference

  • 名称:将值对象改为引用对象 change value to reference
  • 概要:从一个类衍生出许多彼此相等的实例,希望将它们替换为同一个对象。将这个值对象变成引用对象
  • 动机: 你希望给这个对象加入一些可修改数据,并确保对任何一个对象的修改都能影响到所有引用此一对象的地方
  • 做法:
    • 使用replace constructor with factory method
    • 编译,测试
    • 决定由什么对象负责提供访问新对象的途径。
    • 决定这些引用对象应该预先创建好,或是应该动态创建。如果是预先创建好的,必须从内存中将它们读出来,确保它们被需要的时候能够及时加载
    • 修改工厂函数,令它返回引用对象
    • 编译,测试
  • 代码演示

修改之前的代码:

///////////////////////////.h
#ifndef REFACTORDATA_H
#define REFACTORDATA_H
class Customer
{
public:
    Customer(QString name);
    QString getName() const;

private:
    QString m_name;
};

class Order
{
public:
    Order(QString customer);
    QString getCustomerName() const;
    void setCustomer(const QString &customer);

private:
    Customer *m_pcustomer;
};
#endif // REFACTORDATA_H
///////////////////////////.cpp
Order::Order(QString customerName)
{
    m_pcustomer = new Customer(customerName);
}

QString Order::getCustomerName() const
{
    return m_pcustomer->getName();
}

void Order::setCustomer(const QString &customerName)
{
    m_pcustomer = new Customer(customerName);
}

Customer::Customer(QString name)
{
    m_name = name;
}

QString Customer::getName() const
{
    return m_name;
}


///////////////////////////main.cpp
int numberOfOrdersFor(vector<Order *> orders, QString customerName)
{
    int result = 0;
    for (auto iter = orders.begin(); iter != orders.end(); iter++)
    {
        if (!(*iter)->getCustomerName().compare(customerName))
        {
            result++;
        }
    }
    return  result;
}


    vector<Order *> orders;
    int order1number = 0;
    orders.push_back(new Order("Order1"));
    orders.push_back(new Order("Order2"));
    orders.push_back(new Order("Order3"));
    order1number = numberOfOrdersFor(orders, "Order1");
    qDebug() << "order1 number = " << order1number;


1)使用replace constructor with factory method指定唯一创建customer对象接口
2)把原本调用构造函数的地方改为调用工厂函数
3)再把customer构造函数声明为private
4)   使用map来作为唯一customer的来源
5) 修改customer的工厂函数里的customer产生方式,并修改工厂函数名称
修改之后的代码:

///////////////////////////.h
#ifndef REFACTORDATA_H
#define REFACTORDATA_H
#include <QMap>
class Customer
{
public:
    QString getName() const;
    static Customer* GetNamedCustomer(QString name);
    static void LoadCustomers();
private:
    Customer(QString name);
private:
    QString m_name;
    static QMap<QString, Customer*> m_Instance;
};

class Order
{
public:
    Order(QString customer);
    QString getCustomerName() const;
    void setCustomer(const QString &customer);

private:
    Customer *m_pcustomer;

};
#endif // REFACTORDATA_H

///////////////////////////.cpp

Order::Order(QString customerName)
{
    m_pcustomer = Customer::GetNamedCustomer(customerName);
}

QString Order::getCustomerName() const
{
    return m_pcustomer->getName();
}

void Order::setCustomer(const QString &customerName)
{
    m_pcustomer = Customer::GetNamedCustomer(customerName);
}

QMap<QString, Customer*>  Customer::m_Instance;

Customer::Customer(QString name)
{
    m_name = name;
}

QString Customer::getName() const
{
    return m_name;
}

Customer* Customer::GetNamedCustomer(QString name)
{
    return m_Instance[name];
}

void Customer::LoadCustomers()
{
    m_Instance["Test1"] = new Customer("Test1");
    m_Instance["Test2"] = new Customer("Test2");
    m_Instance["Test3"] = new Customer("Test3");
}

猜你喜欢

转载自blog.csdn.net/mafucun1988/article/details/89458384