设计模式--行为模式--策略模式

一、名词解释

  策略模式(Strategy Pattern),如果从设计的本质来看,则像定义了一个接口,后续每个行为封装了实现类。策略模式与模版模式有类似的地方,模版是对每个方法进行封装,策略是对每个类进行扩展。 

    从上边分析:策略模式有着设计模式通用的有点:可扩展(感觉像空话,设计模式都是考虑扩展与开闭原则的);对应的缺点也就是会增加类。

     角色信息:策略模式主要由这三个角色组成,环境角色(Context)、抽象策略角色(Strategy)和具体策略角色(ConcreteStrategy)。

     A:环境角色(Context):持有一个策略类的引用,提供给客户端使用。可以省略

     B:抽象策略角色(Strategy):这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。

     C:具体策略角色(ConcreteStrategy):包装了相关的算法或行为。

二、基本例子:

  模拟一个支付系统的实现,如果用微信调用,则调用财付通的接口;如果转账额度比较大,用人行的大小额系统;如果考虑到手续费低,用中金支付系统实现。

     A:环境角色(Context):

         省略。

     B:抽象策略角色(Strategy):

 1 package comm.pattern.action.strategy;
 2 
 3 /**
 4  * 
 5  * @Title: IEasyPay.java
 6  * @Package: comm.pattern.action.strategy
 7  * @Description: 接口,支付接口
 8  * @author yangzhancheng
 9  * @2020年3月1日:下午11:34:28
10  *
11  */
12 public interface IEasyPay {
13     
14     //支付描述
15     void pay(int amount);
16 
17 }
View Code

 

     C:具体策略角色(ConcreteStrategy):

     C-1: 财付通接口

 1 package comm.pattern.action.strategy;
 2 
 3 /**
 4  * 
 5  * @Title: Tenpay.java
 6  * @Package: comm.pattern.action.strategy
 7  * @Description: 腾讯的财付通支付
 8  * @author yangzhancheng
 9  * @2020年3月1日:下午11:40:02
10  *
11  */
12 public class Tenpay implements IEasyPay {
13 
14     @Override
15     public void pay(int amount) {
16         System.out.println("腾讯的财付通支付:"+amount);
17     }
18 
19 }
View Code

     C-2: 大小额接口

 1 package comm.pattern.action.strategy;
 2 
 3 /**
 4  * 
 5  * @Title: CnapsPay.java
 6  * @Package: comm.pattern.action.strategy
 7  * @Description: 大小额系统(这里当作一个系统看待哈)
 8  * @author yangzhancheng
 9  * @2020年3月1日:下午11:38:37
10  *
11  */
12 public class CnapsPay implements IEasyPay {
13 
14     @Override
15     public void pay(int amount) {
16         System.out.println("大小额支付:"+amount);
17     }
18 
19 }
View Code

     C-3: 中金支付

 1 package comm.pattern.action.strategy;
 2 
 3 
 4 /**
 5  * 
 6  * @Title: CfcaPay.java
 7  * @Package: comm.pattern.action.strategy
 8  * @Description: 中金支付
 9  * @author yangzhancheng
10  * @2020年3月1日:下午11:37:05
11  *
12  */
13 public class CfcaPay implements IEasyPay {
14 
15     @Override
16     public void pay(int amount) {
17         System.out.println("中金支付:"+amount);
18     }
19 
20 }
View Code

     D:客户端类(Client)

 1 package comm.pattern.action.strategy;
 2 
 3 /**
 4  * 
 5  * @Title: Client.java
 6  * @Package: comm.pattern.action.strategy
 7  * @Description: 描述该文件做什么
 8  * @author yangzhancheng
 9  * @2020年3月1日:下午11:48:47
10  *
11  */
12 public class Client {
13     
14     public static void main(String[] args) {
15 
16        //1. 如果是微信环境接入,优先用腾讯的财付通支付(系统代码省略)
17         IEasyPay tenpay = new Tenpay ();
18         tenpay.pay(200);
19         
20         //2.如果金额比较大,走大小额系统
21         IEasyPay cnapsPay = new CnapsPay ();
22         cnapsPay.pay(30000);
23 
24         //3.如果想选择手续费低的,选择中金支付
25         IEasyPay cfcaPay = new CfcaPay ();
26         cfcaPay.pay(250);
27 
28     }
29 
30 
31 }
View Code

    运行结果

腾讯的财付通支付:200
大小额支付:30000
中金支付:250

 

三、总结与比较

     后续待补充。

posted on 2020-03-02 20:25  茫然的法庭  阅读(148)  评论(0)    收藏  举报

导航