Restructuring Reading Notes (II) -- Chapter 1 reconstruction case

Source: Internet
Author: User
Chapter 1 reconstruction: the first case

In the first chapter, the author tries to illustrate the basic process and steps of restructuring through an example of video rental. It can be seen that the author has high hopes for this case and has spent a lot of time. For this reason, I have no reason to study this chapter.

The example of renting a video itself is not difficult, but I spent the whole afternoon Learning this example. First, I honestly copied the code using C ++, and then followed the author's steps to reconstruct it step by step, in order to experience the perfect process of "Rebuilding and improving design.

Despite the slow progress in these three nights, we have gained a lot:

I. Experience the process of restructuring and improving the design.

2. I have a better understanding of State/strategy.

3. I tried to use qtestlib for unit testing for the code for the first time. Although there are still many things to learn, at least I started.

4. A question when I started reading this book? How can I develop my software functions?

Here, the author gives the answer: we should learn to switch back and forth between "refactoring this hat" and "adding new features this hat. Of course, you must always know which hat you are wearing!

Remember the author's advice:

1. Any fool can write code that the computer can understand. Only good programmers can write code that is easy to understand.

2. Is it worthwhile to change the variable name? Definitely !!

3. code readability> performance during refactoring. Performance must be considered only during optimization.

Code:

# Ifndef customer_h # define customer_h # include <qstring> # include <vector> # include "Marshal. H "// customer Class Customer {public: customer (const qstring & name); void apendrental (const Marshal & Marshal); qstring getname () const; qstring Statement () const; qstring htmlstatement () const; private: qstring m_name; STD: vector <Marshal> m_als als; double gettotalamount () const; int getfreqrenterpoints () const ;}; # endif // customer_h # include "customer. H "Customer: customer (const qstring & name): m_name (name) {} void Customer: apendrental (const Marshal & Marshal) {m_externals.push_back (Marshal);} qstring Customer:: getname () const {return m_name;} qstring Customer: Statement () const {qstring result = "Marshal record for" + getname () + "\ n "; for (STD: vector <Marshal >:: size_type I = 0; I <m_externals.size (); ++ I) {Marshal arental = m_externals.at (I ); // Add the entry information result + = "\ t" + arental. getmovie (). gettitle () + "\ t" + qstring: Number (arental. getcharge () + "\ n";} result + = "totalamount is" + qstring: Number (gettotalamount () + "\ n "; result + = "freqrenterpoints is" + qstring: Number (getfreqrenterpoints (); return result;} qstring Customer: htmlstatement () const {qstring result = "HTML Marshal record for" + getname () + "\ n"; for (STD: vector <Marshal >:: size_type I = 0; I <m_marshals.size (); ++ I) {Marshal arental = m_marshals.at (I); // Add entry information result + = "\ t" + arental. getmovie (). gettitle () + "\ t" + qstring: Number (arental. getcharge () + "\ n";} result + = "HTML totalamount is" + qstring: Number (gettotalamount () + "\ n "; result + = "HTML freqrenterpoints is" + qstring: Number (getfreqrenterpoints (); return result;} double customer: gettotalamount () const {double totalamount = 0; // amount for (STD: vector <Marshal >:: size_type I = 0; I <m_externals.size (); ++ I) {Marshal arental = m_externals.at (I ); totalamount + = arental. getcharge () ;}return totalamount;} int Customer: getfreqrenterpoints () const {int freqrenterpoints = 0; // points for (STD: vector <Marshal> :: size_type I = 0; I <m_rentals.size (); ++ I) {Marshal arental = m_rentals.at (I); freqrenterpoints + = arental. getfreqrenterpoints ();} return freqrenterpoints ;}

 

# Ifndef inclual_h # define inclual_h # include "movie. H "// lease class Marshal {public: Marshal (const Movie & movie, int daysrented); movie getmovie () const; int getdaysrented () const; double getcharge () const; int getfreqrenterpoints () const; private: Movie m_movie; int m_daysrented ;};# endif // rental_h # include "Marshal. H "Marshal: Marshal (const Movie & movie, int daysrented): m_movie (movie), m_daysrented (daysrented) {} movie MARSHAL: getmovie () const {return m_movie ;} int partition Al: getdaysrented () const {return m_daysrented;} Double Partition Al: getcharge () const {return m_movie.getcharge (m_daysrented);} int partition Al: getfreqrenterpoints () const {return m_movie.getfreqrenterpoints (m_daysrented );}

 

# Ifndef movie_h # define movie_h # include <qstring> // class movie {public: static const int childrens = 2; static const int regular = 0; static const int newrelease = 1; public: Movie (const qstring & title, int pricecode); void settitle (const qstring & title); qstring gettitle () const; void setpricecode (INT pricecode); int getpricecode () const; double getcharge (INT daysrented) const; int getfreqrenterpoints (INT daysrented) const; private: qstring m_title; int m_pricecode ;}; # endif // movie_h # include "movie. H "movie: Movie (const qstring & title, int pricecode): m_title (title), m_pricecode (pricecode) {} void movie: settitle (const qstring & title) {m_title = title;} qstring movie: gettitle () const {return m_title;} void movie: setpricecode (INT pricecode) {m_pricecode = pricecode;} int movie :: getpricecode () const {return m_pricecode;} double movie: getcharge (INT daysrented) const {double thisamount = 0; // calculated amount switch (getpricecode () {case regular: thisamount + = 2; If (daysrented> 2) thisamount + = (daysrented-2) * 1.5; break; Case newrelease: thisamount + = daysrented * 3; break; Case childrens: thisamount + = 1.5; If (daysrented> 3) thisamount + = (daysrented-3) * 1.5; break; default: // assert (); break;} return thisamount ;} int movie: getfreqrenterpoints (INT daysrented) const {// calculate the point if (m_pricecode = newrelease) & (daysrented> 1) {return 2 ;} else {return 1 ;}}

Refactor to The Strategy Mode:

# Ifndef movie_h # define movie_h # include <qstring> class pricestrategy; // video class movie {public: static const int childrens = 2; static const int regular = 0; static const int newrelease = 1; public: Movie (const qstring & title, int pricecode); void settitle (const qstring & title); qstring gettitle () const; void setpricecode (INT pricecode); int getpricecode () const; double getcharge (INT daysrented) const; int round (INT daysrented) const; private: qstring m_title; pricestrategy * m_pricestrategy ;}; # endif // movie_h # include "movie. H "# include" pricestrategy. H "movie: Movie (const qstring & title, int pricecode): m_title (title) {setpricecode (pricecode);} void movie: settitle (const qstring & title) {m_title = title;} qstring movie: gettitle () const {return m_title;} void movie: setpricecode (INT pricecode) {Switch (pricecode) {case regular: m_pricestrategy = new regularpricestrategy (); break; Case newrelease: m_pricestrategy = new newreleasepricestrategy (); break; Case childrens: m_pricestrategy = new condition (); break; default: // assert (); break;} int movie: getpricecode () const {return m_pricestrategy-> getpricecode ();} double movie: getcharge (INT daysrented) const {return m_pricestrategy-> getcharge (daysrented);} int movie: getfreqrenterpoints (INT daysrented) const {return m_pricestrategy-> getfreqrenterpoints (daysrented );}

Policy code file:

#ifndef PRICESTRATEGY_H#define PRICESTRATEGY_Hclass PriceStrategy{public:    PriceStrategy();    virtual int getPriceCode() const = 0;    virtual double getCharge(int daysRented) const = 0;    virtual int getFreqRenterPoints(int daysRented) const;};class RegularPriceStrategy : public PriceStrategy{public:    RegularPriceStrategy() {}    virtual int getPriceCode() const;    virtual double getCharge(int daysRented) const;};class ChildrenPriceStrategy : public PriceStrategy{public:    ChildrenPriceStrategy() {}    virtual int getPriceCode() const;    virtual double getCharge(int daysRented) const;};class NewReleasePriceStrategy : public PriceStrategy{public:    NewReleasePriceStrategy() {}    virtual int getPriceCode() const;    virtual double getCharge(int daysRented) const;    virtual int getFreqRenterPoints(int daysRented) const;};#endif // PRICESTRATEGY_H#include "pricestrategy.h"#include "movie.h"PriceStrategy::PriceStrategy(){}int PriceStrategy::getFreqRenterPoints(int daysRented) const{    Q_UNUSED(daysRented);    return 1;}int RegularPriceStrategy::getPriceCode() const{    return Movie::REGULAR;}double RegularPriceStrategy::getCharge(int daysRented) const{    double result = 2;    if (daysRented > 2)        result += (daysRented - 2)*1.5;    return result;}int ChildrenPriceStrategy::getPriceCode() const{    return Movie::CHILDRENS;}double ChildrenPriceStrategy::getCharge(int daysRented) const{    double result = 1.5;    if (daysRented > 3)        result += (daysRented - 3)*1.5;    return result;}int NewReleasePriceStrategy::getPriceCode() const{    return Movie::NEWRELEASE;}double NewReleasePriceStrategy::getCharge(int daysRented) const{    return daysRented*3;}int NewReleasePriceStrategy::getFreqRenterPoints(int daysRented) const{    return (daysRented > 1) ? 2 : 1;}

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.