《重构:改善既有代码的设计》代码实践 之 第一章


测试结果:
1. 作者首先构建了一个简单的示例来说明重构的重要性。
影片出租店:计算每一位顾客的消费金额并打印详单。
操作者告诉程序:顾客租了哪些影片,租期多长,程序便根据租赁时间和影片类型算出费用。影片分为三类:普通片,儿童片和新片。除了计算费用,还要为常客计算积分,积分会根据租片种类是否为新片而有不同。

2. 详细代码实现如下

Refector_1.h

#ifndef REFECTOR_1_H
#define REFECTOR_1_H
#include <QString>
#include <QVector>

class Movie
{
public:
    Movie(QString title, int priceCode);
    const static int CHILDREN = 2;
    const static int REGULAR = 0;
    const static int NEW_RELEASE = 1;
    int GetPriceCode();
    void SetPriceCode(int priceCode);
    QString GetTitle();

private:
    QString m_Title;
    int     m_PriceCode;
};



class Rental
{
public:
    Rental(Movie *movie, int daysRented);
    int GetDaysRented();
    Movie* GetMovie();
private:
    Movie* m_Movie;
    int m_DaysRented;


};

class Customer
{
public:
    Customer(QString name);
    void AddRental(Rental *rental);
    QString GetName();
    QVector<Rental *> GetRentals();
    QString Statement();
private:
    QString m_Name;
    QVector<Rental *> m_Rentals;

};

#endif // REFECTOR_1_H
Refector_1.cpp

#include "Refector_1.h"
#include <QDebug>


Movie::Movie(QString title, int priceCode)
{
    m_Title = title;
    m_PriceCode = priceCode;
}

int Movie::GetPriceCode()
{
    return m_PriceCode;
}

void Movie::SetPriceCode(int priceCode)
{
    m_PriceCode = priceCode;
}

QString Movie::GetTitle()
{
    return m_Title;
}

Rental::Rental(Movie *movie, int daysRented)
{
    m_Movie =movie;
    m_DaysRented = daysRented;
}

int Rental::GetDaysRented()
{
    return m_DaysRented;
}

Movie *Rental::GetMovie()
{
    return m_Movie;
}

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

void Customer::AddRental(Rental *rental)
{
    m_Rentals.push_back(rental);
}

QString Customer::GetName()
{
    return  m_Name;
}

QVector<Rental *> Customer::GetRentals()
{
    return  m_Rentals;
}

QString Customer::Statement()
{
    double TotalAmout = 0;
    int FrequentRenterPoints = 0;
    QString result = "Rental Record for " + GetName() + "\n";
    for (Rental *rental : GetRentals())
    {
        double ThisAmount = 0;
        switch (rental->GetMovie()->GetPriceCode())
        {
        case Movie::REGULAR:
            ThisAmount += 2;
            if (rental->GetDaysRented() > 2)
            {
                ThisAmount += (rental->GetDaysRented() - 2)*1.5;
            }
            break;
        case Movie::NEW_RELEASE:
            ThisAmount += rental->GetDaysRented() * 3;
            break;
        case Movie::CHILDREN:
            ThisAmount += 1.5;
            if (rental->GetDaysRented() > 3)
            {
                ThisAmount += (rental->GetDaysRented() - 3)*1.5;
            }
            break;
        }
        FrequentRenterPoints++;
        if (Movie::NEW_RELEASE == (rental->GetMovie()->GetPriceCode()) &&
                rental->GetDaysRented() > 1 )
        {
            FrequentRenterPoints++;
        }
        result += "\t" + rental->GetMovie()->GetTitle() + "\t" + QString("%1").arg(ThisAmount) + "\n";
        TotalAmout += ThisAmount;
    }

    result += "Amount owed is " + QString("%1").arg(TotalAmout) + "\n";
    result += "You earned " + QString("%1").arg(FrequentRenterPoints) + " frequent renter points";
    qDebug() << qPrintable(result) ;
    return  result;
}

main.cpp

    Movie movie1("Movie1", 0);
    Movie movie2("Movie2", 1);
    Movie movie3("Movie3", 2);
    Movie movie4("Movie4", 0);
    Movie movie5("Movie5", 1);
    Movie movie6("Movie6", 2);
    Movie movie7("Movie7", 0);
    Rental rental1(&movie1, 1);
    Rental rental2(&movie2, 2);
    Rental rental3(&movie3, 3);
    Rental rental4(&movie4, 4);
    Rental rental5(&movie5, 5);
    Rental rental6(&movie6, 6);
    Rental rental7(&movie7, 7);
    Customer ctmer("tester");
    ctmer.AddRental(&rental1);
    ctmer.AddRental(&rental2);
    ctmer.AddRental(&rental3);
    ctmer.AddRental(&rental4);
    ctmer.AddRental(&rental5);
    ctmer.AddRental(&rental6);
    ctmer.AddRental(&rental7);
    ctmer.Statement();

执行结果:

3. 代码中出现的问题
1)statement() 函数过长
2)可复用性太差。 显示格式只能为QString, 不能在html或者其他不支持该类型的平台上显示。
4. 重构的方法
1) 分解并重组 statement

  • Extract Method: 将switch 提炼到独立函数中
    • A: 找出局部变量和参数: rental ,  ThisAount
    • B:  将不被修改的变量当成参数出入到新函数
    • C: 如果被修改的只有1个,可以将其变为返回值
    • D: 如果不好区分,就全部传引用
    • F: 重命名函数内的参数或者变量名

修改后的代码

QString Customer::Statement()
{
    double TotalAmout = 0;
    int FrequentRenterPoints = 0;
    QString result = "Rental Record for " + GetName() + "\n";
    for (Rental *rental : GetRentals())
    {
        double ThisAmount = 0;
        ThisAmount = AmountFor(rental);
        FrequentRenterPoints++;
        if (Movie::NEW_RELEASE == (rental->GetMovie()->GetPriceCode()) &&
                rental->GetDaysRented() > 1 )
        {
            FrequentRenterPoints++;
        }
        result += "\t" + rental->GetMovie()->GetTitle() + "\t" + QString("%1").arg(ThisAmount) + "\n";
        TotalAmout += ThisAmount;
    }

    result += "Amount owed is " + QString("%1").arg(TotalAmout) + "\n";
    result += "You earned " + QString("%1").arg(FrequentRenterPoints) + " frequent renter points";
    qDebug() << qPrintable(result) ;
    return  result;
}
double Customer::AmountFor(Rental *rental)
{
    double Result = 0;
    switch (rental->GetMovie()->GetPriceCode())
    {
    case Movie::REGULAR:
        Result += 2;
        if (rental->GetDaysRented() > 2)
        {
            Result += (rental->GetDaysRented() - 2)*1.5;
        }
        break;
    case Movie::NEW_RELEASE:
        Result += rental->GetDaysRented() * 3;
        break;
    case Movie::CHILDREN:
        Result += 1.5;
        if (rental->GetDaysRented() > 3)
        {
            Result += (rental->GetDaysRented() - 3)*1.5;
        }
        break;
    }
    return Result;
}

测试结果:

  • Move Method: 由于AmountFor只用到了rental ,而没有用到customer的任何函数。
    • A: 应该把AmountFor移动到rental类中
    • B:   修改AmountFor的函数名, GetCharge()
    • C:    如果AmountFor使开放给客户的接口,就不应该在customer类中直接调用GetCharge,应该保留AmountFor方法。如果不是,就可以在customer类中直接调用GetCharge。

修改后的代码

double Rental::GetCharge()
{
    double Result = 0;
    switch (GetMovie()->GetPriceCode())
    {
    case Movie::REGULAR:
        Result += 2;
        if (GetDaysRented() > 2)
        {
            Result += (GetDaysRented() - 2)*1.5;
        }
        break;
    case Movie::NEW_RELEASE:
        Result += GetDaysRented() * 3;
        break;
    case Movie::CHILDREN:
        Result += 1.5;
        if (GetDaysRented() > 3)
        {
            Result += (GetDaysRented() - 3)*1.5;
        }
        break;
    }
    return Result;
}
double Customer::AmountFor(Rental *rental)
{
    return rental->GetCharge();
}

测试结果:

 

  • Replace Temp with Query: 去除临时变量
    • ThisAmount是个临时变量,可以直接去除。使用 GetCharge()来代替。
    • 但如果GetCharge()计算很复杂,很耗时间,可以保留ThisAmount这个临时变量
  • Extract Method: 将积分计算抽出来。
    • 积分计算根据影片种类而有不同,可以将其单独成立一个函数
    • 将积分计算函数移动到rental类中

修改后的代码:

QString Customer::Statement()
{
    double TotalAmout = 0;
    int FrequentRenterPoints = 0;
    QString result = "Rental Record for " + GetName() + "\n";
    for (Rental *rental : GetRentals())
    {
        double ThisAmount = 0;
        ThisAmount = AmountFor(rental);
        FrequentRenterPoints += rental->GetFrequentRenterPoints();
        result += "\t" + rental->GetMovie()->GetTitle() + "\t" + QString("%1").arg(ThisAmount) + "\n";
        TotalAmout += ThisAmount;
    }

    result += "Amount owed is " + QString("%1").arg(TotalAmout) + "\n";
    result += "You earned " + QString("%1").arg(FrequentRenterPoints) + " frequent renter points";
    qDebug() << qPrintable(result) ;
    return  result;
}

int Rental::GetFrequentRenterPoints()
{
    if (Movie::NEW_RELEASE == (GetMovie()->GetPriceCode()) &&
            GetDaysRented() > 1 )
    {
        return 2;
    }
    return 1;
}

测试结果:

  • Replace Temp with Query: 去除临时变量
    • 用 GetTotalCharge() 替换 TotalAmount
    • 用GetTotalFrequentRenterPoints() 替换FrequentRenterPoints

修改后的代码:

QString Customer::Statement()
{
    QString result = "Rental Record for " + GetName() + "\n";
    for (Rental *rental : GetRentals())
    {
        result += "\t" + rental->GetMovie()->GetTitle() + "\t" + QString("%1").arg(AmountFor(rental)) + "\n";
    }

    result += "Amount owed is " + QString("%1").arg(GetTotalCharge()) + "\n";
    result += "You earned " + QString("%1").arg(GetTotalFrequentRenterPoints()) + " frequent renter points";
    qDebug() << qPrintable(result) ;
    return  result;
}
double Customer::GetTotalCharge()
{
    double result = 0;
    for (Rental *rental : GetRentals())
    {
        result += AmountFor(rental);
    }
    return result;
}

int Customer::GetTotalFrequentRenterPoints()
{
    int result = 0;
    for (Rental *rental : GetRentals())
    {
        result += rental->GetFrequentRenterPoints();
    }
    return result;
}

测试结果:

  • Move Method: 由于GetCharge/GetFrequentRenterPoints主要用到Movie类中的函数,将两者的变化可以控制在movie类中,将来变化时改动要小一些。
    • 最好不要在另一个对象的属性基础上运用switch语句。如果不得不使用,也应该在对象自己的数据上使用。
    • 将GetCharge移动到Movie类中。
    • 将GetFrequentRenterPoints移动到Movie类中。

修改后的代码:

int Rental::GetFrequentRenterPoints()
{
    return GetMovie()->GetFrequentRenterPoints(GetDaysRented());
}
double Rental::GetCharge()
{
    return GetMovie()->GetCharge(GetDaysRented());
}

int Movie::GetFrequentRenterPoints(int daysRented)
{
    if (NEW_RELEASE == (GetPriceCode()) &&
            daysRented > 1 )
    {
        return 2;
    }
    return 1;
}

double Movie::GetCharge(int daysRented)
{
    double Result = 0;
    switch (GetPriceCode())
    {
    case Movie::REGULAR:
        Result += 2;
        if (daysRented > 2)
        {
            Result += (daysRented - 2)*1.5;
        }
        break;
    case Movie::NEW_RELEASE:
        Result += daysRented * 3;
        break;
    case Movie::CHILDREN:
        Result += 1.5;
        if (daysRented > 3)
        {
            Result += (daysRented - 3)*1.5;
        }
        break;
    }
    return Result;
}

测试结果:

 

  • 运用多态取代与价格相关的条件逻辑
    • 有多种影片类型,他们的计费方式和积分方式不同。我们可以用继承机制来表现不同的影片类型
    • 用多态来取代switch语句。但是此处不可以。因为一个对象在创建后,其作为子类的特性就不能再修改了。
    • 使用状态模式/策略模式来解决这个问题
    • 先运用 Replace Type Code with State/Stategy,将类型相关的行为搬移至state/Stategy模式中。
      • 使用Self Encapsulate Field,确保任何时候都通过取值函数(getter)和设置函数(setter)来访问类型代码。
    • 然后运用MoveMethod将switch语句移到Price类。
    • 最后运用Replace Conditional with Polymorphism去掉switch 语句



修改后的代码:

Refector_1.h
#ifndef REFECTOR_1_H
#define REFECTOR_1_H
#include <QString>
#include <QVector>

class Price
{
public:
    virtual int GetPriceCode() = 0;
    virtual double GetCharge(int daysRented) = 0;
};

class ChildrenPrice : public Price
{
public:
    int GetPriceCode();
    double GetCharge(int daysRented);
};

class NewReleasePrice : public Price
{
public:
    int GetPriceCode();
    double GetCharge(int daysRented);
};

class RegularPrice : public Price
{
public:
    int GetPriceCode();
    double GetCharge(int daysRented);
};

class ExceptionPrice : public Price
{
public:
    ExceptionPrice();
    int GetPriceCode();
    double GetCharge(int daysRented);
};


class Movie
{
public:
    Movie(QString title, int priceCode);
    const static int CHILDREN = 2;
    const static int REGULAR = 0;
    const static int NEW_RELEASE = 1;
    int GetPriceCode();
    void SetPriceCode(int priceCode);
    QString GetTitle();
    double GetCharge(int daysRented);
    int  GetFrequentRenterPoints(int daysRented);

private:
    QString m_Title;
    int     m_PriceCode;
    Price *m_pPrice;
};



class Rental
{
public:
    Rental(Movie *movie, int daysRented);
    int GetDaysRented();
    double GetCharge();
    Movie* GetMovie();
    int GetFrequentRenterPoints();

private:
    Movie* m_Movie;
    int m_DaysRented;


};

class Customer
{
public:
    Customer(QString name);
    void AddRental(Rental *rental);
    QString GetName();
    QVector<Rental *> GetRentals();
    QString Statement();
    double AmountFor(Rental *rental);
    double GetTotalCharge();
    int GetTotalFrequentRenterPoints();
private:
    QString m_Name;
    QVector<Rental *> m_Rentals;

};

#endif // REFECTOR_1_H
Refector_1.cpp

#include "Refector_1.h"
#include <QDebug>


Movie::Movie(QString title, int priceCode)
{
    m_Title = title;
    SetPriceCode(priceCode);
}

int Movie::GetPriceCode()
{
    return m_pPrice->GetPriceCode();
}

void Movie::SetPriceCode(int priceCode)
{
    switch (priceCode) {
    case REGULAR:
        m_pPrice = new RegularPrice();
        break;
    case CHILDREN:
        m_pPrice = new ChildrenPrice();
        break;
    case NEW_RELEASE:
        m_pPrice = new NewReleasePrice();
        break;
    default:
        m_pPrice = new ExceptionPrice();
    }
}

QString Movie::GetTitle()
{
    return m_Title;
}

double Movie::GetCharge(int daysRented)
{
    return m_pPrice->GetCharge(daysRented);
}

int Movie::GetFrequentRenterPoints(int daysRented)
{
    if (NEW_RELEASE == (GetPriceCode()) &&
            daysRented > 1 )
    {
        return 2;
    }
    return 1;
}

Rental::Rental(Movie *movie, int daysRented)
{
    m_Movie =movie;
    m_DaysRented = daysRented;
}

int Rental::GetDaysRented()
{
    return m_DaysRented;
}

double Rental::GetCharge()
{
    return GetMovie()->GetCharge(GetDaysRented());
}

Movie *Rental::GetMovie()
{
    return m_Movie;
}

int Rental::GetFrequentRenterPoints()
{
    return GetMovie()->GetFrequentRenterPoints(GetDaysRented());
}

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

void Customer::AddRental(Rental *rental)
{
    m_Rentals.push_back(rental);
}

QString Customer::GetName()
{
    return  m_Name;
}

QVector<Rental *> Customer::GetRentals()
{
    return  m_Rentals;
}

QString Customer::Statement()
{
    QString result = "Rental Record for " + GetName() + "\n";
    for (Rental *rental : GetRentals())
    {
        result += "\t" + rental->GetMovie()->GetTitle() + "\t" + QString("%1").arg(AmountFor(rental)) + "\n";
    }

    result += "Amount owed is " + QString("%1").arg(GetTotalCharge()) + "\n";
    result += "You earned " + QString("%1").arg(GetTotalFrequentRenterPoints()) + " frequent renter points";
    qDebug() << qPrintable(result) ;
    return  result;
}

double Customer::AmountFor(Rental *rental)
{
    return rental->GetCharge();
}

double Customer::GetTotalCharge()
{
    double result = 0;
    for (Rental *rental : GetRentals())
    {
        result += AmountFor(rental);
    }
    return result;
}

int Customer::GetTotalFrequentRenterPoints()
{
    int result = 0;
    for (Rental *rental : GetRentals())
    {
        result += rental->GetFrequentRenterPoints();
    }
    return result;
}


int ChildrenPrice::GetPriceCode()
{
    return Movie::CHILDREN;
}

double ChildrenPrice::GetCharge(int daysRented)
{
    double Result = 0;
    Result += 1.5;
    if (daysRented > 3)
    {
        Result += (daysRented - 3)*1.5;
    }
    return Result;
}

int NewReleasePrice::GetPriceCode()
{
    return Movie::NEW_RELEASE;
}

double NewReleasePrice::GetCharge(int daysRented)
{
    return daysRented * 3;
}

int RegularPrice::GetPriceCode()
{
    return Movie::REGULAR;
}

double RegularPrice::GetCharge(int daysRented)
{
    double Result = 0;
    Result += 2;
    if (daysRented > 2)
    {
        Result += (daysRented - 2)*1.5;
    }
    return Result;
}

ExceptionPrice::ExceptionPrice()
{
    //trigger except handler
    qDebug() << "Except occur";
}

int ExceptionPrice::GetPriceCode()
{
    return -1;
}

double ExceptionPrice::GetCharge(int daysRented)
{
    return 0;
}

5. 参考文献
1) 重构:改善既有代码的设计, Martin Fowler ,熊节译, 中国工信出版集团,人民邮电出版社。

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页