行為型模式--策略模式(Strategy)
阿新 • • 發佈:2019-01-04
一:定義:
Strategy:Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
二:引入
假設現在要設計一個販賣各類書籍的電子商務網站的購物車(Shopping Cat)系統。一個最簡單的情況就是把所有貨品的單價乘上數量,但是實際情況肯定比這要複雜。
比如:
- 本網站可能對所有的兒童類圖書實行每本一元的折扣;
- 對計算機類圖書提供每本7%的促銷折扣,而對電子類圖書有3%的折扣;
- 對其餘的圖書沒有折扣。
- 未來可能還會有新的打折策略。
由於有這樣複雜的折扣演算法,使得價格計算問題需要系統地解決。
方案一:業務邏輯放在各具體子類
/**各子類實現銷售價格演算法
*/
publicabstractclass Book {
privatedouble price;
private String name;
public String getName() {
return name;
}
publicvoid setName(String name) {
}
publicdouble getPrice() {
return price;
}
publicvoid setPrice(double price) {
this.price = price;
}
publicabstractdouble getSalePrice() ;
}
publicclass CsBook extends Book{
public CsBook(String name,double price)
{
this
this.setPrice(price);
}
publicdouble getSalePrice()
{
returnthis.getPrice()*0.93;
}
}
publicclass ChildrenBook extends Book {
public ChildrenBook(String name, double price) {
this.setName(name);
this.setPrice(price);
}
publicdouble getSalePrice() {
returnthis.getPrice() -1;
}
}
publicclass Client {
publicstaticvoid main(String args[])
{
Book csBook1=new CsBook("Think in java",45);
Book childrenBook1=new ChildrenBook("Hello",20);
System.out.println(csBook1.getName()+":"+csBook1.getSalePrice());
System.out.println(childrenBook1.getName()+":"+childrenBook1.getSalePrice());
}
}
問題:每個子類必須都各自實現打折演算法,即使打折演算法相同。所以code reuse不好
方案二:
//把打折策略程式碼提到父類來實現code reusepublicabstractclass Book {
privatedouble price;
private String name;
public String getName() {
return name;
}
publicvoid setName(String name) {
this.name = name;
}
publicdouble getPrice() {
return price;
}
publicvoid setPrice(double price) {
this.price = price;
}
// 銷售價格
publicstaticdouble toSalePrice(Book book)
{
if (book instanceof ChildrenBook)
{
return book.getPrice()-1;
}
elseif (book instanceof CsBook)
{
return book.getPrice()*0.93;
}
return0;
}
}
publicclass Client {
publicstaticvoid main(String args[])
{
Book csBook1=new CsBook("Think in java",45);
Book childrenBook1=new ChildrenBook("Hello",20);
System.out.println(csBook1.getName()+":"+Book.toSalePrice(csBook1));
System.out.println(childrenBook1.getName()+":"+Book.toSalePrice(childrenBook1));
}
}
toSalePrice方法是比較容易change的地方,如果策略複雜用if判斷比較亂,並且策略修改或增加時需改變原始碼。
方案三:策略模式
code reuse時最好用合成(HAS-A)而不用(IS-A),更加靈活。
publicabstractclass Book {privatedouble price;
private String name;
private DiscountStrategy discountStrategy;//折扣策略
public String getName() {
return name;
}
publicvoid setName(String name) {
this.name = name;
}
publicdouble getPrice() {
return price;
}
publicvoid setPrice(double price) {
this.price = price;
}
public DiscountStrategy getDiscountStrategy() {
return discountStrategy;
}
publicvoid setDiscountStrategy(DiscountStrategy discountStrategy) {
this.discountStrategy = discountStrategy;
}
publicdouble getSalePrice()
{
return discountStrategy.getSalePrice(price);
}
}
publicclass CsBook extends Book{
public CsBook(String name,double price)
{
this.setName(name);
this.setPrice(price);
}
}
publicabstractclass DiscountStrategy {
publicabstractdouble getSalePrice(double orgPrice) ;
}
/*
* 按折扣率打折
*/
publicclass PercentDiscountStrategy extends DiscountStrategy {
privatedouble percent;//折扣率
public PercentDiscountStrategy(double percent)
{
this.percent=percent;
}
publicdouble getPercent() {
return percent;