设计模式-建造者模式
概述
将一个复杂对象的构建与其表示分离,将一个复杂对象的构建过程抽象出来。
当构造函数参数有多个,且这些参数可选,考虑使用建造者模式。因为它的构建过程比较复杂,
建造者模式与工厂模式的区别:两者都是用来创建对象,封装复杂创建过程,但是建造者模式可以让客户端参与建造过程
案例:
这是我在项目中应用建造者模式的案例,CheckoutPageGenerator参数多,且对象创建过程复杂,所以使用Builder分离建造过程
成员
CheckoutPageGenerator:页面生成器,购物流程页面的内容都从此类获取
Builder:用于生成CheckoutPageGenerator
代码
public class CheckoutPageGenerator
{
protected long UserID = 0;
protected string UserGuid = string.Empty;
protected string ShopCartIDs = string.Empty;
protected string CouponCode = string.Empty;
protected string CurrencyName = string.Empty;
protected string CurrencySymbol = string.Empty;
protected long ShipID = 0;
protected long ShipAddressID = 0;
private CheckoutPageGenerator(ILifetimeScope serviceLocator)
{
}
/// <summary>
/// 加载购物流程页面数据
/// </summary>
/// <param name="loadType"></param>
/// <returns></returns>
public CheckoutResponseModel LoadPageData(ECheckoutLoadType loadType, long shipId = 0)
{
xxxxxx
}
/// <summary>
/// 获取购物车产品
/// </summary>
/// <returns></returns>
public List<ShopCartInnerProductModel> GetShopCartList()
{
xxxxxx
}
/// <summary>
/// 执行活动
/// </summary>
/// <returns></returns>
public ShopCartDoActiveResult ExecuteActive(bool isExecuteShip = true)
{
xxxxxx
}
/// <summary>
/// 执行活动
/// </summary>
/// <returns></returns>
private ShopCartDoActiveResult ExecuteActive(List<ShopCartInnerProductModel> shopCartList)
{
xxxxxx
}
/// <summary>
/// 获取ShipInfo
/// </summary>
private ShipResult GetShipInfo(List<ShopCartInnerProductModel> shopCartList)
{
xxxxxx
}
/// <summary>
/// 加载运费列表
/// </summary>
private bool LoadShipList(List<ShopCartInnerProductModel> shopCartList, CheckoutResponseModel checkoutResponseModel, long shipId = 0)
{
xxxxxx
}
/// <summary>
/// 用于创建CheckoutPageGenerator
/// </summary>
public class Builder
{
public long UserID = 0;
public string UserGuid = string.Empty;
public string Token = string.Empty;
public string ShopCartIDs = string.Empty;
public string CouponCode = string.Empty;
public bool IsUseCoupon = true;
public ILifetimeScope ServiceLocator = null;
public Builder(ILifetimeScope serviceLocator)
{
this.ServiceLocator = serviceLocator;
}
public Builder SetUserID(long userID)
{
this.UserID = userID;
return this;
}
public Builder SetUserGuid(string userGuid)
{
this.UserGuid = userGuid;
return this;
}
public Builder SetToken(string token)
{
this.Token = token;
return this;
}
public Builder SetCouponCode(string couponCode)
{
this.CouponCode = couponCode;
if (string.IsNullOrWhiteSpace(couponCode))
{
this.IsUseCoupon = false;
}
return this;
}
public Builder SetShopCartIDs(string shopCartIDs)
{
this.ShopCartIDs = shopCartIDs;
return this;
}
public CheckoutPageGenerator Build()
{
long userID = this.UserID;
string userGuid = this.UserGuid;
string couponCode = this.CouponCode;
string shopCartIDs = this.ShopCartIDs;
string currencyName = string.Empty;
string currencySymbol = string.Empty;
long shipID = 0;
long shipAddressID = 0;
if (!string.IsNullOrWhiteSpace(this.Token))
{
Cart_checkout checkout = this.ServiceLocator.Resolve<ShopCartBus>().GetCheckout(this.Token);
if (checkout != null)
{
userID = checkout.UserID;
userGuid = checkout.UserGuid;
shopCartIDs = checkout.ShopCartIDs;
currencyName = checkout.Currency;
currencySymbol = checkout.CurrencySymbol;
shipID = checkout.ShipID;
shipAddressID = checkout.ShipAddressID;
if (this.IsUseCoupon)
{
couponCode = !string.IsNullOrWhiteSpace(couponCode) ? couponCode : checkout.CouponCode;
}
else
{
couponCode = string.Empty;
}
//购物流程使用完优惠券后直接更新优惠券信息
checkout.CouponCode = couponCode;
this.ServiceLocator.Resolve<ShopCartBus>().UpdateCheckOut(checkout);
}
}
else
{
System_currency currency = this.ServiceLocator.Resolve<SystemCurrencyBus>().GetSiteCurrencyInfo();
if (currency != null)
{
currencyName = currency.Name;
currencySymbol = currency.Symbols;
}
}
var checkoutPage = new CheckoutPageGenerator(this.ServiceLocator);
checkoutPage.UserID = userID;
checkoutPage.UserGuid = userGuid;
checkoutPage.CouponCode = couponCode;
checkoutPage.ShopCartIDs = shopCartIDs;
checkoutPage.CurrencyName = currencyName;
checkoutPage.CurrencySymbol = currencySymbol;
checkoutPage.ShipID = shipID;
checkoutPage.ShipAddressID = shipAddressID;
return checkoutPage;
}
}
}
客户端:
var pageGenerator = this._pageBuilder.Value
.SetUserID(base.UserID)
.SetUserGuid(base.UserGuid)
.Build();
建造者模式和工厂模式的区别
它们目的都是创建一个复杂的对象
工厂模式注重的是整体对象的创建方法,而建造者模式注重的是对象的创建过程,创建对象的过程方法可以在创建时自由调用。
看一下建造者模式的例子就明白了:
public class EmployeeBuilder
{
private int id = 1;
private string firstname = "first";
private string lastname = "last";
private DateTime birthdate = DateTime.Today;
private string street = "street";
public Employee Build()
{
return new Employee(id, firstname, lastname, birthdate, street);
}
public EmployeeBuilder WithFirstName(string firstname)
{
this.firstname = firstname;
return this;
}
public EmployeeBuilder WithLastName(string lastname)
{
this.lastname = lastname;
return this;
}
public EmployeeBuilder WithBirthDate(DateTime birthdate)
{
this.birthdate = birthdate;
return this;
}
public EmployeeBuilder WithStreet(string street)
{
this.street = street;
return this;
}
public static implicit operator Employee(EmployeeBuilder instance)
{
return instance.Build();
}
}
调用:
void main(){
Employee emp1 = new EmployeeBuilder().WithFirstName("Kenneth")
.WithLastName("Truyers");
Employee emp2 = new EmployeeBuilder().WithBirthDate(new DateTime(1983, 1,1));
}