1、策略模式:策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,
只是实现不同,他可以以相同的方式来调用所有的算法,减少了各种算法类与使用算法类之间的耦合。
2、题目:做一个商场收银软件,营业员根据客户所购买的商品的单价和数量,向客户收费,收费模式分三种:第一种,正常收费;
第二种,打8折;第三种,满300送100;
3、UML图:
4、示例代码:
4.1、抽象算法类:
CashSuper类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.aibaiyang.idemo.service.strategy; /** * @Author zhong guo * @Date 2019/8/30 11:44 * @description 抽象算法类 **/ public abstract class CashSuper { //现金收取算法方法 public abstract double acceptCash(double money); }
CashNormal类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.aibaiyang.idemo.service.strategy; /** * @Author zhong guo * @Date 2019/8/30 11:50 * @description 正常收费 **/ public class CashNormal extends CashSuper{ @Override public double acceptCash(double money) { return money; } }
CashRebate类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.aibaiyang.idemo.service.strategy; import lombok.Data; /** * @Author zhong guo * @Date 2019/8/30 11:51 * @description 打折收费子类 **/ @Data public class CashRebate extends CashSuper{ //折扣率 private double moneyRebate; //构造方法 public CashRebate(double moneyRebate){ this.moneyRebate = moneyRebate; } @Override public double acceptCash(double money) { return money * moneyRebate; } }
CashReturn类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.aibaiyang.idemo.service.strategy; import lombok.AllArgsConstructor; import lombok.Data; import lombok.experimental.Accessors; /** * @Author zhong guo * @Date 2019/8/30 11:56 * @description 返利类子类 **/ @Data @AllArgsConstructor @Accessors(chain = true) public class CashReturn extends CashSuper{ //返利条件 private double moneyCondition; //返利值 private double moneyReturn; @Override public double acceptCash(double money) { //若钱大于返利条件,则减去返利值 if(money >= moneyCondition){ money = money - moneyReturn; } return money; } }
4.2、上下文类:
CashContext类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.aibaiyang.idemo.service.strategy; /** * @Author zhong guo * @Date 2019/8/30 12:34 * @description **/ public class CashContext { private CashSuper cs; public CashContext(Integer type){ switch (type){ case 1: cs = new CashNormal(); break; case 2: cs = new CashRebate(0.8); break; case 3: cs = new CashReturn(300f ,100f); break; } } /** * 得到结果 * @param money * @return */ public double getResult(double money){ return cs.acceptCash(money); } }
4.3、其它相关类:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.aibaiyang.idemo.enums; /** * 策略模式枚举类型 */ public enum StrategyEnum { ONE(1,"正常收费"), TWO(2,"打8折收费"), THREE(3,"满300返100收费"); private Integer type; private String description; StrategyEnum(Integer type, String description) { this.type = type; this.description = description; } public Integer getType() { return type; } public void setType(Integer type) { this.type = type; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
4.4、策略模式客户端:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
package com.aibaiyang.idemo.service.strategy; import com.aibaiyang.idemo.enums.StrategyEnum; /** * @Author zhong guo * @Date 2019/8/30 12:46 * @description 策略模式客户端 **/ public class StrategyClient { public static void main(String[] args) { CashContext cashContext = new CashContext(StrategyEnum.ONE.getType()); double totalMoney = cashContext.getResult(253f * 3); System.out.println(totalMoney); } }
5、github: idemo-design-parttern/service/strategy