????????????
??????????????????????????????е??????????. ????????????????????????????????????????????.??Щ????е??????е??????????????????????????????????????????????.
???????????????????????????????????????????.??????????????????????????????????????Щ?????ú??????.
????????????????????????????????????????????????????????????????????????????????????????????н??????.??????????????????????????????????????????????????????????????????????????????????????.
???????????
????????2-8?????????80%????????????????20%???????????. ???????????????????????????ЩС??????????????. ???????????????????????????????????. ???????????????????????????????????????.
????????????????????е??????????????з????????????????е?????????????????????????????????????:
????· ?????? (rename)
????· ???? (extract)
????· ???? (inline)
????· ??? (move)
???????????????????????????????????????????????????????C/C++?????????????.
?????????????????????????????????????????????????????????????????????????????????????????????????????.
???????????е?????????????????????????????????????????????л?????????.
???????糣???Self Encapsulate Field(???????)??????????????Extract Method.
??????????????????Replace Condition with Polymorphism(???????????????)??????Extract Method ?? Move Method????.
????????????????????????????????????????????????????????????????????????????.????????????????????????????????????????.
??????????
????????????????????????????????????????????????????????????????仯?????????????????????Щ????????? ??????????????????????????????????????????????????????????????????????????????????.
??????????????????????????????仯????????????С????????????????趼???????.????????????????????????????????????????γ??Щ??????.??????????????????????????.?????????????????????????????????.
????????????????????????????????????????????????????????:
????1??setup
????· ???????????????μ???????. ????:?????????????????????
????· ?′???????????????к??????????????λ?ú??????.???????????????
????· ?μ????????????????copy??д?????copy?????????????????????.(?????copy)
????· ??????????????????У????????д??????????κ????.
????· ???????????????????????
????2??substitute
????· ????????1???′????????????滻????д???
????· ?????????????????滻???????????е??????????
????· ??????????????滻; ?Щ?????????????滻??????????????”ê??”
????· ??????????????????????????????????滻???????????????????????
??????????1??2???????У???????????????????????????. ??????1??2????????????裬???????????????????????????????????????.????????????????????????????任(????θ???)??????????????????????????????????д????????????.??????????????????????????????????????????????????????????????????????. ?????????????????????????????????????????????????????????裬?????????????????????????С??????????С?????????????????????????????.
?????????????????????????????????????????:
??????????????(Rename Variable)
????unsigned int start = Date::getTime();
????// load program ...
????unsigned int offset = Date::getTime() - start;
????cout << "Load time was: " << offset/1000 << "seconds" << endl;
???????????????????У?????offset???????????????????????????elapsed????????????????????setup??????????μ????eclapsed??????????????offset.???????????????????????????????????const.
????unsigned int start = Date::getTime();
????// load program ...
????unsigned int offset = Date::getTime() - start;
????const unsigned int elapsed = offset;
????cout << "Load time was: " << offset/1000 << "seconds" << endl;
??????????????????????????????1. ??????????У???????????????μ??????????????????д???.?????????????????????????????. ????????????д??????????????????.
???????????????????????substitute????????????滻?????????????????.?????????????????????????offset????.????????????????????滻?????.
????unsigned int start = Date::getTime();
????// load program ...
????unsigned int offset = Date::getTime() - start;
????const unsigned int elapsed = offset;
????cout << "Load time was: " << elapsed/1000 << "seconds" << endl;
????????????????????????滻:
????unsigned int start = Date::getTime();
????// load program ...
????const unsigned int elapsed = Date::getTime() - start;
????cout << "Load time was: " << elapsed/1000 << "seconds" << endl;
?????????滻?????????в??????????????????????.
???????????????У????????start??elapsed?????и???????????????????????????????????????λ???????????? startMs???elapsedMs????????????????滻. ????????д??????????1000?????????г??????????????extract variable??????????????????1000???滻.
???????????? (Extract Method)
????void printAmount(const Items& items)
????{
????int sum = 0;
????for(auto item : items)
????{
????sum += item.getValue();
????}
????cout << "The amount of items is " << sum << endl;
????}
??????????????????????£???????????items?????????????value???????????????д??.
????????????????????????????????????calcAmount?????????????????Items?????????.??????Extract Method?????????????????????????setup.
???????????calcAmount??????????
????int calcAmount(const Items& items)
????{
????return 0;
????}
????void printAmount(const Items& items)
????{
????int sum = 0;
????for(auto item : items)
????{
????sum += item.getValue();
????}
????cout << "The amount of items is " << sum << endl;
????}
?????????????calcAmount?????????.???????????????????????copy??calcAmount?в???????.?м???????????1?в????????????????????????ü??У????????????????????????????????????????????????????????????!
????int calcAmount(const Items& items)
????{
????int sum = 0;
????for(auto item : items)
????{
????sum += item.getValue();
????}
????return sum;
????}
????void printAmount(const Items& items)
????{
????int sum = 0;
????for(auto item : items)
????{
????sum += item.getValue();
????}
????cout << "The amount of items is " << sum << endl;
????}
??????????????????1???OK??????????б?????????????????????????.
???????????????????????substitute.???o???calcAmount?滻????????Items????????????.?????????????????????????printAmount????(???????????????????Items???????????????????д??????).
????int calcAmount(const Items& items)
????{
????int sum = 0;
????for(auto item : items)
????{
????sum += item.getValue();
????}
????return sum;
????}
????void printAmount(const Items& items)
????{
????cout << "The amount of items is " << calcAmount(items) << endl;
????}
?????滻??????в???.???????????Extract Method????????.
???????????????????????????????????????Move Method??calcAmount????????Items???У???????Rename Method??????????????getAmount?????. ????Move Method??Rename Method????????????????????????????????????. ????Move Mehod???????????????????setup????Items???д???public???????calcAmount?????????????????copy???????????????OK. ???????????????substitute?????′?????Items????????滻???CalcAmount??????OK???????????Move Method???.
???????????Ч?????£????????????.
????struct Items
????{
????int getAmount() const;
????...
????};
????void printAmount(const Items& items)
????{
????cout << "The amount of items is " << items.getAmount() << endl;
????}
????????????????н?printAmount?????Items?У??????getAmount???Items???????????.??????δ?????????????????????????′???????????????н???????Items??.
?????????????????????????????????????????????????????. ?????????????е?rename???????extract?????????C++ IDE???????????????????????????????????????????????????????Ч????.?????????????????????????????.????C++??????????????????????????????????????????????????????????????????????????????????(??????к?????????????????Extract Method)???????????????????????????????. ????????C++??????????Щ???????????(????ruby)?????????????????.????????????????????????????????????????裬??????Ч????????.
???????????????????????????! ?????????о????????????????????????????.???????????????????????????????????????????????????????????.
???????????Martin?????????”Replace Type Code with Class(?????????????)”????????????????????????裬??????????е????????:
????· ??????????????
????· ?????????????????????????
????· ????????
????· ???????????????????????????????????????????????o?????????????
????· ???????????????????????????
????· ????????????????????
????· ????????????????????????????????????????
????· ????????
?????????????????в???1????????setup??????2????????substitute(??3??????2???????????????1???????????????????). ????4??????setup????5????????substitute. ??????????????????????У???????????????????????????????£????????????????????????????????. ???????????????????????????????????????????????????????????????????????. ???????????裬?????????????????????????????????????????????????.
????ê??????
??????????????????substitute????????????????滻?????????????ê??.
??????????????????????????滻.???????IDE?????????????????????????е??????.????????????????????IDE??????????????.???????C++?????????????????IDE??????????. ????????????????????????滻/??????????????.
??????νê??????????????м???????ж?????????????????????????????????????????????????????????滻.????滻?????????ê????????inline???????????ê??.
???????????????????????????ê??Encapsulate field????????Σ???Replace Constructor with Factory Method(??????????????????)??
????Encapsulate field??????????????????ε???????????????????á????磺Move Field(???????????)??Replace Type Code with Subclasses???????????????????Encapsulate field???????????δ???????ê???????????ó????ε?????????????????ε???????????滻??????????????????????????????????????????
????Setup???????????????????????????????ε????????(??????????????д??get??set???????)??????
????Substitute????????????????????ε????滻????????????. ????????滻?????get??????????滻?????set????в????
???????????Encapsulate field??????????????????ε???????????????????????get??set???????????ε????????????????
???????????”Move Field(???????????)”??????????????????????????Encapsulate field????????????δ???????ê??. ???????Encapsulate field????????????????????ε??????????????????????????д????????Σ????????get??set???????????????ε?????????????????е????. ??????????????????????????. ???????????????????get??set??????????inline???.
???????????ê???????????????????????????????м???????????????滻???????. ê?????ü????滻???????????????滻??????.
???????Щ?????????ê???и?????????壬?Щ?????????????ê?????????????????Щ????????????! ??????? Replace Constructor with Factory Method(??????????????????)??????????????棬??????????ê???????????????????????????棬????????????????????????????????????Щ??????????????????.
?????????????????????????????????ê??????
// Shoes.h
enum Type
{
REGULAR??
NEW_STYLE??
LIMITED_EDITION
};
struct Shoes
{
Shoes(Type type?? double price);
double getCharge(int quantity) const;
private:
Type type;
double price;
};
// Shoes.cpp
#include "Shoes.h"
Shoes::Shoes(Type type?? double price)
: type(type)?? price(price)
{
}
double Shoes::getCharge(int quantity) const
{
double result = 0;
switch(type)
{
case REGULAR:
result += price * quantity;
break;
case NEW_STYLE:
if(quantity > 1)
{
result += (price + (quantity - 1) * price * 0.8);
}
else
{
result += price;
}
break;
case LIMITED_EDITION:
result += price * quantity * 1.1;
break;
}
return result;
}
??????????????????Shoes??????type?????????Shoes?????????????REGULAR??NEW_STYLE????LIMITED_EDITION??Shoes?????getCharge????????????quantity??????????á?getCharge??????????????????????м????????????REGULAR?????????????????????????????NEW_STYLE???????????????????????????棨LIMITED_EDITION??????????????10%????á?