策略模式+工厂模式优化if...else if...else if结构
2018-01-16 10:50
423 查看
转载自:http://blog.csdn.net/u011507568/article/details/70238491
首先,按照if...else if语句来实现打折商品的例子,代码如下:
[java] view
plain copy
public class Example {
public Double calRecharge(Double charge ,RechargeTypeEnum type ){
if(type.equals(RechargeTypeEnum.E_BANK)){
return charge*0.85;
}else if(type.equals(RechargeTypeEnum.BUSI_ACCOUNTS)){
return charge*0.90;
}else if(type.equals(RechargeTypeEnum.MOBILE)){
return charge;
}else if(type.equals(RechargeTypeEnum.CARD_RECHARGE)){
return charge+charge*0.01;
}else{
return null;
}
}
}
用策略+工厂模式优化:
枚举类:
[java] view
plain copy
package strategy;
public enum RechargeTypeEnum {
E_BANK(1, "网银"),
BUSI_ACCOUNTS(2, "商户账号"),
MOBILE(3,"手机卡充值"),
CARD_RECHARGE(4,"充值卡");
private int value;
private String description;
private RechargeTypeEnum(int value, String description) {
this.value = value;
this.description = description;
}
public int value() {
return value;
}
public String description() {
return description;
}
public static RechargeTypeEnum valueOf(int value) {
for(RechargeTypeEnum type : RechargeTypeEnum.values()) {
if(type.value() == value) {
return type;
}
}
return null;
}
}
策略接口:
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public interface Strategy {
public Double calRecharge(Double charge ,RechargeTypeEnum type );
}
策略具体实现类:
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class EBankStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge*0.85;
}
}
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class BusiAcctStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
// TODO Auto-generated method stub
return charge*0.90;
}
}
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class MobileStrategy implements Strategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
// TODO Auto-generated method stub
return charge;
}
}
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class CardStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge+charge*0.01;
}
}
策略上下文:
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class Context {
private Strategy strategy;
public Double calRecharge(Double charge, Integer type) {
strategy = StrategyFactory.getInstance().creator(type);
return strategy.calRecharge(charge, RechargeTypeEnum.valueOf(type));
}
public Strategy getStrategy() {
return strategy;
}
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
}
工厂类:
[java] view
plain copy
package strategy.strategy;
import java.util.HashMap;
import java.util.Map;
import strategy.RechargeTypeEnum;
public class StrategyFactory {
private static StrategyFactory factory = new StrategyFactory();
private StrategyFactory(){
}
private static Map strategyMap = new HashMap<>();
static{
strategyMap.put(RechargeTypeEnum.E_BANK.value(), new EBankStrategy());
strategyMap.put(RechargeTypeEnum.BUSI_ACCOUNTS.value(), new BusiAcctStrategy());
strategyMap.put(RechargeTypeEnum.MOBILE.value(), new MobileStrategy());
strategyMap.put(RechargeTypeEnum.CARD_RECHARGE.value(), new CardStrategy());
}
public Strategy creator(Integer type){
return strategyMap.get(type);
}
public static StrategyFactory getInstance(){
return factory;
}
}
客户端类:
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class Client {
public static void main(String[] args) {
Context context = new Context();
// 网银充值100 需要付多少
Double money = context.calRecharge(100D,
RechargeTypeEnum.E_BANK.value());
System.out.println(money);
// 商户账户充值100 需要付多少
Double money2 = context.calRecharge(100D,
RechargeTypeEnum.BUSI_ACCOUNTS.value());
System.out.println(money2);
// 手机充值100 需要付多少
Double money3 = context.calRecharge(100D,
RechargeTypeEnum.MOBILE.value());
System.out.println(money3);
// 充值卡充值100 需要付多少
Double money4 = context.calRecharge(100D,
RechargeTypeEnum.CARD_RECHARGE.value());
System.out.println(money4);
}
}
运行结果:
85.0
90.0
100.0
101.0
从上面代码可以看出,策略模式把具体的算法封装到了具体策略角色内部,增强了可扩展性,隐蔽了实现细节;它替代继承来实现,避免了if-
else这种不易维护的条件语句。当然我们也可以看到,策略模式由于独立策略实现,使得系统内增加了很多策略类;对客户端来说必须知道兜友哪些具体策略, 而且需要知道选择具体策略的条件。
首先,按照if...else if语句来实现打折商品的例子,代码如下:
[java] view
plain copy
public class Example {
public Double calRecharge(Double charge ,RechargeTypeEnum type ){
if(type.equals(RechargeTypeEnum.E_BANK)){
return charge*0.85;
}else if(type.equals(RechargeTypeEnum.BUSI_ACCOUNTS)){
return charge*0.90;
}else if(type.equals(RechargeTypeEnum.MOBILE)){
return charge;
}else if(type.equals(RechargeTypeEnum.CARD_RECHARGE)){
return charge+charge*0.01;
}else{
return null;
}
}
}
用策略+工厂模式优化:
枚举类:
[java] view
plain copy
package strategy;
public enum RechargeTypeEnum {
E_BANK(1, "网银"),
BUSI_ACCOUNTS(2, "商户账号"),
MOBILE(3,"手机卡充值"),
CARD_RECHARGE(4,"充值卡");
private int value;
private String description;
private RechargeTypeEnum(int value, String description) {
this.value = value;
this.description = description;
}
public int value() {
return value;
}
public String description() {
return description;
}
public static RechargeTypeEnum valueOf(int value) {
for(RechargeTypeEnum type : RechargeTypeEnum.values()) {
if(type.value() == value) {
return type;
}
}
return null;
}
}
策略接口:
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public interface Strategy {
public Double calRecharge(Double charge ,RechargeTypeEnum type );
}
策略具体实现类:
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class EBankStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge*0.85;
}
}
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class BusiAcctStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
// TODO Auto-generated method stub
return charge*0.90;
}
}
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class MobileStrategy implements Strategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
// TODO Auto-generated method stub
return charge;
}
}
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class CardStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge+charge*0.01;
}
}
策略上下文:
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class Context {
private Strategy strategy;
public Double calRecharge(Double charge, Integer type) {
strategy = StrategyFactory.getInstance().creator(type);
return strategy.calRecharge(charge, RechargeTypeEnum.valueOf(type));
}
public Strategy getStrategy() {
return strategy;
}
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
}
工厂类:
[java] view
plain copy
package strategy.strategy;
import java.util.HashMap;
import java.util.Map;
import strategy.RechargeTypeEnum;
public class StrategyFactory {
private static StrategyFactory factory = new StrategyFactory();
private StrategyFactory(){
}
private static Map strategyMap = new HashMap<>();
static{
strategyMap.put(RechargeTypeEnum.E_BANK.value(), new EBankStrategy());
strategyMap.put(RechargeTypeEnum.BUSI_ACCOUNTS.value(), new BusiAcctStrategy());
strategyMap.put(RechargeTypeEnum.MOBILE.value(), new MobileStrategy());
strategyMap.put(RechargeTypeEnum.CARD_RECHARGE.value(), new CardStrategy());
}
public Strategy creator(Integer type){
return strategyMap.get(type);
}
public static StrategyFactory getInstance(){
return factory;
}
}
客户端类:
[java] view
plain copy
package strategy.strategy;
import strategy.RechargeTypeEnum;
public class Client {
public static void main(String[] args) {
Context context = new Context();
// 网银充值100 需要付多少
Double money = context.calRecharge(100D,
RechargeTypeEnum.E_BANK.value());
System.out.println(money);
// 商户账户充值100 需要付多少
Double money2 = context.calRecharge(100D,
RechargeTypeEnum.BUSI_ACCOUNTS.value());
System.out.println(money2);
// 手机充值100 需要付多少
Double money3 = context.calRecharge(100D,
RechargeTypeEnum.MOBILE.value());
System.out.println(money3);
// 充值卡充值100 需要付多少
Double money4 = context.calRecharge(100D,
RechargeTypeEnum.CARD_RECHARGE.value());
System.out.println(money4);
}
}
运行结果:
85.0
90.0
100.0
101.0
从上面代码可以看出,策略模式把具体的算法封装到了具体策略角色内部,增强了可扩展性,隐蔽了实现细节;它替代继承来实现,避免了if-
else这种不易维护的条件语句。当然我们也可以看到,策略模式由于独立策略实现,使得系统内增加了很多策略类;对客户端来说必须知道兜友哪些具体策略, 而且需要知道选择具体策略的条件。
相关文章推荐
- 策略模式+工厂模式优化if...else if...else if结构
- 策略模式+工厂模式优化if...else if...else if结构
- 策略模式+工厂模式优化if...else if...else if结构
- 【C语言简单说】八:分支结构之if...else if()...else...(3)
- JavaScript之基础-6 JavaScript 分支结构 (if、if-else、else if、switch-case)
- java语言基础-if-else if-else结构
- 过长的if-else分支结构优化
- 如何证明if--else if--else结构执行顺序
- C#中的分支结构:if..else if 、if... else 和 switch ...case语句的区别
- C#分支结构if,if-else,if-else if,switch-case区别
- C language 使用if…else if…else语句实现多分支选择结构*(语法结构同C#)
- 第十节:控制结构-简洁-if-else-elseif/else if-流程控制的替代语法
- 条件分支结构(switch-case)(if-else if-else) 2011.05.09
- if-else优化措施
- ======================.Net中的选择结构 if if -else ========================
- Linux 下的 shell 编程之 if-else选择结构
- 代码优化——去除你代码中的if...else...层层嵌套
- 用if...else结构判断a除以2的余数是否为1来确定a值得奇偶性
- 实验三——for语句及分支结构else-if
- 过多if-else分支的优化