Skip to content

Instantly share code, notes, and snippets.

@hatelove
Created March 23, 2012 05:57
Show Gist options
  • Save hatelove/2167459 to your computer and use it in GitHub Desktop.
Save hatelove/2167459 to your computer and use it in GitHub Desktop.
20120323 ASP.NET: Refactoring to Patterns sample
//v8
protected void btnCalculate_Click(object sender, EventArgs e)
{
//若頁面通過驗證
if (this.IsValid)
{
//取得畫面資料
var product = this.GetProduct();
ILogistics logistics = FactoryRepository.GetILogistics(this.drpCompany.SelectedValue, product);
if (logistics != null)
{
logistics.Calculate();
var companyName = logistics.GetsComapanyName();
var fee = logistics.GetsFee();
//呈現結果
this.SetResult(companyName, fee);
}
else
{
var js = "alert('發生不預期錯誤,請洽系統管理者');location.href='http://tw.yahoo.com/';";
this.ClientScript.RegisterStartupScript(this.GetType(), "back", js, true);
}
}
}
/// <summary>
/// 呈現結果
/// </summary>
/// <param name="companyName"></param>
/// <param name="fee"></param>
private void SetResult(string companyName, double fee)
{
this.lblCompany.Text = companyName;
this.lblCharge.Text = fee.ToString();
}
/// <summary>
/// 取得畫面資料
/// </summary>
/// <returns></returns>
private Product GetProduct()
{
var result = new Product
{
Name = this.txtProductName.Text.Trim(),
Weight = Convert.ToDouble(this.txtProductWeight.Text),
Size = new Size()
{
Length = Convert.ToDouble(this.txtProductLength.Text),
Width = Convert.ToDouble(this.txtProductWidth.Text),
Height = Convert.ToDouble(this.txtProductHeight.Text)
},
IsNeedCool = this.rdoNeedCool.SelectedValue == "1"
};
return result;
}
//v8
namespace RefactoringSampleLibrary
{
public class BlackCat : RefactoringSampleLibrary.ILogistics
{
private double _fee;
private readonly string _companyName = "黑貓";
public Product ShipProduct { get; set; }
public void Calculate()
{
var weight = this.ShipProduct.Weight;
//計算運費邏輯
if (weight > 20)
{
this._fee = 500;
}
else
{
//頁面呈現計算的運費結果
var fee = 100 + weight * 10;
this._fee = fee;
}
}
public string GetsComapanyName()
{
return this._companyName;
}
public double GetsFee()
{
return this._fee;
}
}
public class Hsinchu : ILogistics
{
private double _fee;
private readonly string _companyName = "新竹貨運";
public void Calculate()
{
var length = this.ShipProduct.Size.Length;
var width = this.ShipProduct.Size.Width;
var height = this.ShipProduct.Size.Height;
var size = length * width * height;
//計算運費邏輯
//長 x 寬 x 高(公分)x 0.0000353
if (length > 100 || width > 100 || height > 100)
{
this._fee = size * 0.0000353 * 1100 + 500;
}
else
{
this._fee = size * 0.0000353 * 1200;
}
}
public Product ShipProduct { get; set; }
public string GetsComapanyName()
{
return this._companyName;
}
public double GetsFee()
{
return this._fee;
}
}
public class PostOffice : ILogistics
{
private double _fee;
private readonly string _companyName = "郵局";
public void Calculate()
{
var weight = this.ShipProduct.Weight;
var feeByWeight = 80 + weight * 10;
var length = this.ShipProduct.Size.Length;
var width = this.ShipProduct.Size.Width;
var height = this.ShipProduct.Size.Height;
var size = length * width * height;
var feeBySize = size * 0.0000353 * 1100;
//計算運費邏輯
if (feeByWeight < feeBySize)
{
this._fee = feeByWeight;
}
else
{
this._fee = feeBySize;
}
}
public Product ShipProduct { get; set; }
public string GetsComapanyName()
{
return this._companyName;
}
public double GetsFee()
{
return this._fee;
}
}
}
namespace RefactoringSampleLibrary
{
public class FactoryRepository
{
/// <summary>
/// 將ILogistics的instance,交給工廠來決定
/// </summary>
/// <param name="company"></param>
/// <param name="product"></param>
/// <returns></returns>
public static 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;
}
}
}
}
namespace RefactoringSampleLibrary
{
public interface ILogistics
{
void Calculate();
string GetsComapanyName();
double GetsFee();
}
}
//v7
/// <summary>
///GetILogistics 的測試
///</summary>
[TestMethod()]
public void GetILogisticsTest_GetBlackCat()
{
//arrange
string p = "1";
Product product = new Product();
ILogistics expected = new BlackCat();
ILogistics actual;
//act
actual = FactoryRepository.GetILogistics(p, product);
//assert
Assert.AreEqual(expected.GetType(), actual.GetType());
}
/// <summary>
///GetILogistics 的測試
///</summary>
[TestMethod()]
public void GetILogisticsTest_Get新竹貨運()
{
//arrange
string p = "2";
Product product = new Product();
ILogistics expected = new Hsinchu();
ILogistics actual;
//act
actual = FactoryRepository.GetILogistics(p, product);
//assert
Assert.AreEqual(expected.GetType(), actual.GetType());
}
/// <summary>
///GetILogistics 的測試
///</summary>
[TestMethod()]
public void GetILogisticsTest_Get郵局()
{
//arrange
string p = "3";
Product product = new Product();
ILogistics expected = new PostOffice();
ILogistics actual;
//act
actual = FactoryRepository.GetILogistics(p, product);
//assert
Assert.AreEqual(expected.GetType(), actual.GetType());
}
//v3 Unit test
/// <summary>
///GetsComapanyName 的測試
///</summary>
[TestMethod()]
public void GetsComapanyNameTest_v3()
{
BlackCat target = new BlackCat();
string expected = "黑貓";
string actual;
actual = target.GetsComapanyName();
Assert.AreEqual(expected, actual);
}
/// <summary>
///GetsFee 的測試
///</summary>
[TestMethod()]
public void GetsFeeTest_v3()
{
BlackCat target = new BlackCat();
double expected = 0F;
double actual;
actual = target.GetsFee();
Assert.AreEqual(expected, actual);
}
/// <summary>
///Calculate 的測試
///</summary>
[TestMethod()]
public void CalculateTest_v3()
{
//從整合測試的test case,來當做單元測試的test case
//arrange
BlackCat target = new BlackCat()
{
ShipProduct = new Product
{
IsNeedCool = true,
Name = "商品測試1",
Size = new Size
{
Height = 10,
Length = 30,
Width = 20
},
Weight = 10
}
};
//act
target.Calculate();
var expectedName = "黑貓";
var expectedFee = 200;
var actualName = target.GetsComapanyName();
var actualFee = target.GetsFee();
//assert
Assert.AreEqual(expectedName, actualName);
Assert.AreEqual(expectedFee, actualFee);
}
}
/// <summary>
///Calculate 的測試
///</summary>
[TestMethod()]
public void CalculateTest_v3()
{
Hsinchu target = new Hsinchu()
{
ShipProduct = new Product
{
IsNeedCool = true,
Name = "商品測試1",
Size = new Size
{
Height = 10,
Length = 30,
Width = 20
},
Weight = 10
}
};
//act
target.Calculate();
//assert
var expectedName = "新竹貨運";
var expectedFee = 254.16;
var actualName = target.GetsComapanyName();
var actualFee = target.GetsFee();
//assert
Assert.AreEqual(expectedName, actualName);
Assert.AreEqual(expectedFee, actualFee);
}
/// <summary>
///GetsComapanyName 的測試
///</summary>
[TestMethod()]
public void GetsComapanyNameTest_v3()
{
Hsinchu target = new Hsinchu();
string expected = "新竹貨運";
string actual;
actual = target.GetsComapanyName();
Assert.AreEqual(expected, actual);
}
/// <summary>
///GetsFee 的測試
///</summary>
[TestMethod()]
public void GetsFeeTest_v3()
{
Hsinchu target = new Hsinchu();
double expected = 0F;
double actual;
actual = target.GetsFee();
Assert.AreEqual(expected, actual);
}
/// <summary>
///Calculate 的測試
///</summary>
[TestMethod()]
public void CalculateTest_v3()
{
PostOffice target = new PostOffice()
{
ShipProduct = new Product
{
IsNeedCool = true,
Name = "商品測試1",
Size = new Size
{
Height = 10,
Length = 30,
Width = 20
},
Weight = 10
}
};
//act
target.Calculate();
//assert
var expectedName = "郵局";
var expectedFee = 180;
var actualName = target.GetsComapanyName();
var actualFee = target.GetsFee();
//assert
Assert.AreEqual(expectedName, actualName);
Assert.AreEqual(expectedFee, actualFee);
}
/// <summary>
///GetsFee 的測試
///</summary>
[TestMethod()]
public void GetsFeeTest_v3()
{
PostOffice target = new PostOffice();
double expected = 0F;
double actual;
actual = target.GetsFee();
Assert.AreEqual(expected, actual);
}
/// <summary>
///GetsComapanyName 的測試
///</summary>
[TestMethod()]
public void GetsComapanyNameTest_v3()
{
PostOffice target = new PostOffice();
string expected = "郵局";
string actual;
actual = target.GetsComapanyName();
Assert.AreEqual(expected, actual);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment