????????????
????????????????????????????????????????????е????????????????????. ??????ж?????????????????????????????????????????????????????????????ú?????.
?????????????????????????????????????! ???????????????????????????????y?????????????.
????????????????????????????????????????????????Extract Method??????????????????????????????????Pull Up Method??????????????.
?????????????????????????????????????????????????????????????????????????????????:??????????????????????????????????????????????????????Ч????????????????????????? ????飬????????????.
?????????????????????Extract Method????????????????????????????.
???????????Extract Method??????н??????????????????????????.???????????????????????????????????д?????κ???Σ???????????????????????????????Ч??.
???????????????????????????????????????????????н??????????????????.
??????????????????????????????????????????????(???????????)?????????????????????????????????????????????????????.???????????????????????????????. ?????????????????????????????????????.??????????????????????????????θ???????????????????????????????????????.
??????????????????????????Extract Method?????????:
?????????: ???????????????滻??????е???????????????!
??????????1???????????????????????????????????????·??????÷???????????????????????????????????????????????OK.????????????????????????????????.
??????????2???????????????????????????????????????????????????copy????????????????copy??????????????????o??????????????????.
??????????3??????????д????б??????????????????t??к??????????????.
???????????4???????????????к????е????????. ???????е????????????????????滻????????(?????????:??????????????).
??????????5???????????????????????????????????????????.????????????????θ??????????????????????????????????????????????.??????????????????弴?????????????????const????.
????????6????????????????????????????????????Σ??????????????????????y?????.????????????????????λ?y??е????????俿????????λ??.(??C?????????????????).
????????????????????????????????????????????Extract Method??????????????????:
????· ?????????????????????????壬???俿?????????????????????????弴?????;
????· ??????????????????.???????????ж?θ?????????????????????????.??????????????????????.
????· ????????????????????????const?????б???.
????· ????”??????????????”????????????в??????.???????????????????????.
????· ?????μ??????????????.
????· ????????д??????????????μ?????У?????????????????????.
????· ???μ?????滻???????????????????.
?????????????????:
????bool calcPrice(Product* products??U32 num??double* totalPrice)
????{
????U32 i;
????double basePrice;
????double discountFactor;
????if(products != NULL)
????{
????for(i = 0; i < num; i++)
????{
????basePrice = products[i].price * products[i].quantity;
????if(basePrice >= 1000)
????{
????discountFactor = 0.95;
????}
????else
????{
????discountFactor = 0.99;
????}
????basePrice *= discountFactor;
????*totalPrice += basePrice;
????}
????return true;
????}
????return false;
????}
?????????????C???????. ????calcPrice????????????product????price. ???????????Product????????飬?????num. ???product??????????????????????????????????. ????????????????????1000?????????0.95??????????0.99. ????totalPrice?????????????product???????. ??????????????true????????false????????totalPrice???.
????Product???????????壬????????
????typedef unsigned int U32;
????struct Product
????{
????double price;
????U32 quantity;
????};
????calcPrice?????????????е?.?????????Extract Method??????????С????.????????????????д??if/for???ε?????????????????????????.????????????????????????????????if??????????for???????????????????????С???????????????????????o???????????????????.
?????????????????eclipse?????????????for????????????????????????????:
????extractExample.jpeg
?????????????????μ?????????5?????????.
??????????????????????????????????Extract Method?????????????.
????????????????????嵽?????????????????????????弴?????;
????bool calcPrice(Product* products??U32 num??double* totalPrice)
????{
????if(products != NULL)
????{
????for(U32 i = 0; i < num; i++)
????{
????double basePrice = products[i].price * products[i].quantity;
????double discountFactor;
????if(basePrice >= 1000)
????{
????discountFactor = 0.95;
????}
????else
????{
????discountFactor = 0.99;
????}
????basePrice *= discountFactor;
????*totalPrice += basePrice;
????}
????return true;
????}
????return false;
????}
???????????????У??????????i??basePrice??dicountFactor?????λ????????????????????.????i??basePrice????????弴?????.
???????????????ж?θ?????????????????????????.??????????????????????.
??????????????????totalPrice??i???????????????????????.??????????basePrice?????θ???????????????????????????????(????actualPrice)?????????????????????.???????в??????????????const??????.
????bool calcPrice(const Product* products??const U32 num??double* totalPrice)
????{
????if(products != NULL)
????{
????for(U32 i = 0; i < num; i++)
????{
????const double basePrice = products[i].price * products[i].quantity;
????double discountFactor;
????if(basePrice >= 1000)
????{
????discountFactor = 0.95;
????}
????else
????{
????discountFactor = 0.99;
????}
????const double actualPrice = basePrice * discountFactor;
????*totalPrice += actualPrice;
????}
????return true;
????}
????return false;
????}
????????”??????????????”????????????в??????.???????????????????????.
???????????????????С??basePrice???.
????double getBasePrice(const Product* product)
????{
????return product->price * product->quantity;
????}
????bool calcPrice(const Product* products??const U32 num??double* totalPrice)
????{
????if(products != NULL)
????{
????for(U32 i = 0; i < num; i++)
????{
????double discountFactor;
????if(getBasePrice(&products[i]) >= 1000)
????{
????discountFactor = 0.95;
????}
????else
????{
????discountFactor = 0.99;
????}
????const double actualPrice = getBasePrice(&products[i]) * discountFactor;
????*totalPrice += actualPrice;
????}
????return true;
????}
????return false;
????}
??????????discountFactor
????double getBasePrice(const Product* product)
????{
????return product->price * product->quantity;
????}
????double getDiscountFactor(const Product* product)
????{
????return (getBasePrice(product) >= 1000) ? 0.95 : 0.99;
????}
????bool calcPrice(const Product* products??const U32 num??double* totalPrice)
????{
????if(products != NULL)
????{
????for(U32 i = 0; i < num; i++)
????{
????const double actualPrice = getBasePrice(&products[i]) * getDiscountFactor(&products[i]);
????*totalPrice += actualPrice;
????}
????return true;
????}
????return false;
????}
????????????actualPrice:
????double getBasePrice(const Product* product)
????{
????return product->price * product->quantity;
????}
????double getDiscountFactor(const Product* product)
????{
????return (getBasePrice(product) >= 1000) ? 0.95 : 0.99;
????}
????double getPrice(const Product* product)
????{
????return getBasePrice(product) * getDiscountFactor(product);
????}
????bool calcPrice(const Product* products??const U32 num??double* totalPrice)
????{
????if(products != NULL)
????{
????for(U32 i = 0; i < num; i++)
????{
????*totalPrice += getPrice(&products[i]);
????}
????return true;
????}
????return false;
????}
??????????????????????????????.????????getBasePrice???ù??????????????????????getDiscountFactor??getPrice?????????inline function???????????????????.????getBasePrice????????????????÷???????????????????.???????????????????????????????????????????????.
?????????????????????calcPrice????????????:
????double getTotalPrice(const Product* products??const U32 num)
????{
????double result = 0;
????for(U32 i = 0; i < num; i++)
????{
????result += getPrice(&products[i]);
????}
????return result;
????}
????bool calcPrice(const Product* products??const U32 num??double* totalPrice)
????{
????if(products == NULL) return false;
????*totalPrice = getTotalPrice(products??num);
????return true;
????}
??????????????getTotalPrice???????????????. ?????????????????????к?????????????????????????????????????????????.??????????????????ú????????????????:
????Product products[10];
????...
????printf("The total price of products is %fn"??getTotalPrice(product??10));
?????????????????????????????????????????????. ????????????????????????????ú?????У?????????????????????????????????????????????????????????????????????ü????????????????.??????????????????????????????????????????.
???????
????????????????????????????????.?????????????????????????????????????. ???????????????????????????裬?????????????????????н?????. ?????????????????輰??????????????????????????. ??????????????С???????????????????????????????λ????. ????substitute??????У???????????ê??????侭?????????. ????????????????????????????????????????????????????????????????.