DAY 17
4

## [Day 17]Refactoring - Strategy Pattern

1. 計算運費
2. 取得運費結果
3. 取得物流商名稱

PS: 拖著殘廢的右手（肩旋轉袖肌腱炎），手抬不起來也要把鍵盤放在大腿上，硬生生把這一篇文章寫完...真心希望對各位讀者在開發上可以有所幫助。
＠目前的程式碼

``````protected void btnCalculate_Click(object sender, EventArgs e)
{
//若頁面通過驗證
if (this.IsValid)
{
//取得畫面資料
var product = this.GetProduct();

var companyName = "";
double fee = 0;

//選黑貓，計算出運費
if (this.drpCompany.SelectedValue == "1")
{
//計算
//BlackCat blackCat = new BlackCat() { ShipProduct = product };
//blackCat.Calculate();
//companyName = blackCat.GetsComapanyName();
//fee = blackCat.GetsFee();
ILogistics logistics = new BlackCat() { ShipProduct = product };
logistics.Calculate();
companyName = logistics.GetsComapanyName();
fee = logistics.GetsFee();

}
//選新竹貨運，計算出運費
else if (this.drpCompany.SelectedValue == "2")
{
//計算
//Hsinchu hsinchu = new Hsinchu() { ShipProduct = product };
//hsinchu.Calculate();
//companyName = hsinchu.GetsComapanyName();
//fee = hsinchu.GetsFee();

ILogistics logistics = new Hsinchu() { ShipProduct = product };
logistics.Calculate();
companyName = logistics.GetsComapanyName();
fee = logistics.GetsFee();
}
//選郵局，計算出運費
else if (this.drpCompany.SelectedValue == "3")
{
//計算
//PostOffice postOffice = new PostOffice() { ShipProduct = product };
//postOffice.Calculate();
//companyName = postOffice.GetsComapanyName();
//fee = postOffice.GetsFee();

ILogistics logistics = new PostOffice() { ShipProduct = product };
logistics.Calculate();
companyName = logistics.GetsComapanyName();
fee = logistics.GetsFee();
}
//發生預期以外的狀況，呈現警告訊息，回首頁
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "back", js, true);
}

//呈現結果
this.SetResult(companyName, fee);
}
}
``````

＠前言

＠重構第九式：運用Design Pattern-策略模式

``````protected void btnCalculate_Click(object sender, EventArgs e)
{
//若頁面通過驗證
if (this.IsValid)
{
//取得畫面資料
var product = this.GetProduct();

var companyName = "";
double fee = 0;

////選黑貓，計算出運費
//if (this.drpCompany.SelectedValue == "1")
//{
//    //計算
//    //BlackCat blackCat = new BlackCat() { ShipProduct = product };
//    //blackCat.Calculate();
//    //companyName = blackCat.GetsComapanyName();
//    //fee = blackCat.GetsFee();
//    ILogistics logistics = new BlackCat() { ShipProduct = product };
//    logistics.Calculate();
//    companyName = logistics.GetsComapanyName();
//    fee = logistics.GetsFee();

//}
////選新竹貨運，計算出運費
//else if (this.drpCompany.SelectedValue == "2")
//{
//    //計算
//    //Hsinchu hsinchu = new Hsinchu() { ShipProduct = product };
//    //hsinchu.Calculate();
//    //companyName = hsinchu.GetsComapanyName();
//    //fee = hsinchu.GetsFee();

//    ILogistics logistics = new Hsinchu() { ShipProduct = product };
//    logistics.Calculate();
//    companyName = logistics.GetsComapanyName();
//    fee = logistics.GetsFee();
//}
////選郵局，計算出運費
//else if (this.drpCompany.SelectedValue == "3")
//{
//    //計算
//    //PostOffice postOffice = new PostOffice() { ShipProduct = product };
//    //postOffice.Calculate();
//    //companyName = postOffice.GetsComapanyName();
//    //fee = postOffice.GetsFee();

//    ILogistics logistics = new PostOffice() { ShipProduct = product };
//    logistics.Calculate();
//    companyName = logistics.GetsComapanyName();
//    fee = logistics.GetsFee();
//}
////發生預期以外的狀況，呈現警告訊息，回首頁
//else
//{
//    this.ClientScript.RegisterStartupScript(this.GetType(), "back", js, true);
//}

ILogistics logistics = this.GetILogistics(this.drpCompany.SelectedValue, product);
if (logistics != null)
{
logistics.Calculate();
companyName = logistics.GetsComapanyName();
fee = logistics.GetsFee();

//呈現結果
this.SetResult(companyName, fee);
}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "back", js, true);
}
}
}

/// <summary>
/// 將ILogistics的instance，交給工廠來決定
/// </summary>
/// <param name="p"></param>
/// <param name="product"></param>
/// <returns></returns>
private ILogistics GetILogistics(string company, Product product)
{
if (company == "1")
{
return new BlackCat() { ShipProduct = product };
}
else if (company == "2")
{
return new Hsinchu() { ShipProduct = product };
}
else if (company == "3")
{
return new PostOffice() { ShipProduct = product };
}
else
{
return null;
}
}
``````

1. 如果頁面Validation通過驗證
2. 取得頁面上商品資訊
3. 取得對應的物流商
4. 請物流商計算運費
5. 取得物流商名稱
6. 取得運費結果
7. 將名稱與運費結果呈現到頁面上

the strategy pattern (also known as the policy pattern) is a particular software design pattern, whereby algorithms can be selected at runtime

＠小結

30天快速上手TDD31

### 2 則留言

0
nestor
iT邦新手 4 級 ‧ 2012-10-25 14:52:11

91大這系列文章會不會像賽大一樣有完整版阿....

0
ted99tw
iT邦高手 1 級 ‧ 2012-10-25 15:24:01