您的位置:首页 > 其它

设计模式之:策略模式(strategy)

2016-01-27 14:10 246 查看
策略模式是一种行为模式。用于某一个具体的项目有多个可供选择的算法策略,客户端在其运行时根据不同需求决定使用某一具体算法策略。

策略模式也被称作政策模式。实现过程为,首先定义不同的算法策略,然后客户端把算法策略作为它的一个参数。使用这种模式最好的例子是Collection.sort()方法了,它使用Comparator对象作为参数。根据Comparator接口不同实现,对象会被不同的方法排序。详细介绍请看java中的排序对象

本文例子是,完成一个简单地购物车,两种付款策略可供选择,一为信用卡,另外一种为Paypal。

首先创建策略接口,在本文例子中,付款金额作为参数。

1
package
com.journaldev.design.strategy;
2
3
public
interface
PaymentStrategy
{
4
5
public
void
pay(
int
amount);
6
}
现在实现使用信用卡及Paypal两种算法策略的实体类。

01
package
com.journaldev.design.strategy;
02
03
public
class
CreditCardStrategy
implements
PaymentStrategy
{
04
05
private
String
name;
06
private
String
cardNumber;
07
private
String
cvv;
08
private
String
dateOfExpiry;
09
10
public
CreditCardStrategy(String
nm,String ccNum,String cvv,String expiryDate){
11
this
.name=nm;
12
this
.cardNumber=ccNum;
13
this
.cvv=cvv;
14
this
.dateOfExpiry=expiryDate;
15
}
16
@Override
17
public
void
pay(
int
amount)
{
18
System.out.println(amount
+
"
paid with credit/debit card"
);
19
}
20
21
}
01
package
com.journaldev.design.strategy;
02
03
public
class
PaypalStrategy
implements
PaymentStrategy
{
04
05
private
String
emailId;
06
private
String
password;
07
08
public
PaypalStrategy(String
email,String pwd){
09
this
.emailId=email;
10
this
.password=pwd;
11
}
12
13
@Override
14
public
void
pay(
int
amount)
{
15
System.out.println(amount
+ 
"
paid using Paypal."
);
16
}
17
18
}
此时,算法策略已经准备就绪,现在需要实现购物车以及能够运用付款策略的支付方法。

01
package
com.journaldev.design.strategy;
02
03
public
class
Item
{
04
05
private
String
upcCode;
06
private
int
price;
07
08
public
Item(String
upc,
int
cost){
09
this
.upcCode=upc;
10
this
.price=cost;
11
}
12
13
public
String
getUpcCode() {
14
return
upcCode;
15
}
16
17
public
int
getPrice()
{
18
return
price;
19
}
20
21
}
01
package
com.journaldev.design.strategy;
02
03
import
java.text.DecimalFormat;
04
import
java.util.ArrayList;
05
import
java.util.List;
06
07
public
class
ShoppingCart
{
08
09
//List
of items
10
List<Item>
items;
11
12
public
ShoppingCart(){
13
this
.items=
new
ArrayList<Item>();
14
}
15
16
public
void
addItem(Item
item){
17
this
.items.add(item);
18
}
19
20
public
void
removeItem(Item
item){
21
this
.items.remove(item);
22
}
23
24
public
int
calculateTotal(){
25
int
sum
=
0
;
26
for
(Item
item : items){
27
sum
+= item.getPrice();
28
}
29
return
sum;
30
}
31
32
public
void
pay(PaymentStrategy
paymentMethod){
33
int
amount
= calculateTotal();
34
paymentMethod.pay(amount);
35
}
36
}
注意,购物车的支付方法接受支付策略作为参数,但是不在其内部保存任何实例变量。

一个简单地测试程序。

01
package
com.journaldev.design.strategy;
02
03
public
class
ShoppingCartTest
{
04
05
public
static
void
main(String[]
args) {
06
ShoppingCart
cart =
new
ShoppingCart();
07
08
Item
item1 =
new
Item(
"1234"
,
10
);
09
Item
item2 =
new
Item(
"5678"
,
40
);
10
11
cart.addItem(item1);
12
cart.addItem(item2);
13
14
//pay
by paypal
15
cart.pay(
new
PaypalStrategy(
"myemail@example.com"
,
"mypwd"
));
16
17
//pay
by credit card
18
cart.pay(
new
CreditCardStrategy(
"Pankaj
Kumar"
,
"1234567890123456"
,
"786"
,
"12/15"
));
19
}
20
21
}
输出如下:

1
50
paid
using Paypal.
2
50
paid
with credit/debit card
策略模式UML图



重要点:

* 此处可以构建策略的实体变量,但是应该尽量避免这种情况。因为需要保证对于特定的任务能够对应某个具体的算法策略,与Collection.sort()Array.sort()方法使用comparator作为参数道理类似。

* 策略模式类似与状态模式。两者之间的不同,状态模式中的Context(环境对象)包含了状态的实例变量,并且不同的任务依赖同一个状态。相反,在策略模式中策略是作为一个参数传递进方法中,context(环境对象)不需要也不能存储任何变量。

* 当一组算法对应一个任务,并且程序可以在运行时灵活的选择其中一个算法,策略模式是很好的选择。

这就是全部的Java策略模式,希望你喜欢上它了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: