栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用策略模式优化IF ELSE

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用策略模式优化IF ELSE

使用传统的if else扩展性不强,代码量越多阅读起来越困难

如果后期又要扩展条件语句维护起来就会变得非常的麻烦

  
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入a,b,c,d四个字符中的任意一个");
        String traditionIfElse = scanner.next();
        //四层if else 看起来还好 但是如果代码量的增加就会导致阅读性越来越低
        if ("a".equals(traditionIfElse)){
            System.out.println("a");
        }else if ("b".equals(traditionIfElse)){
            System.out.println("b");
        }else if ("c".equals(traditionIfElse)){
            System.out.println("c");
        }else if ("d".equals(traditionIfElse)){
            System.out.println("d");
        }else {
            System.out.println("瞎瘠薄输入");
        }
    }

执行效果 

 

因此我们可以用策略模式来优化if else的代码让它变得更加的精简

统一的访问路径

public interface StrategyContext {
    
    String strategyTest();
}

A实现类 

public class AtypeImplementation implements StrategyContext {

    @Override
    public String strategyTest() {return "a";}
}

B实现类

public class BtypeImplementation implements StrategyContext {

    @Override
    public String strategyTest(){return "b";}
}

C实现类

public class CtypeImplementation implements StrategyContext {

    @Override
    public String strategyTest() {
        return "c";
    }
}

D实现类

public class DtypeImplementation implements StrategyContext {

    @Override
    public String strategyTest() {
        return "d";
    }
}

 在使用枚举类型做工厂模式

public enum StrategyEnum {
    a("a",new AtypeImplementation()),
    b("b",new BtypeImplementation()),
    c("c",new CtypeImplementation()),
    d("d",new DtypeImplementation())
    ;

    private String text;
    private StrategyContext strategyContext;

    StrategyEnum(String text, StrategyContext strategyContext) {
        this.text = text;
        this.strategyContext = strategyContext;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public StrategyContext getStrategyContext() {
        return strategyContext;
    }

    public void setStrategyContext(StrategyContext strategyContext) {
        this.strategyContext = strategyContext;
    }
}

 main测试

public class StrategyIfElse {
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入a,b,c,d四个字符中的任意一个");
        String traditionIfElse = scanner.next();
        //使用枚举值优化if else
        StrategyContext strategyContext = StrategyEnum.valueOf(traditionIfElse).getStrategyContext();
        System.out.println(strategyContext.strategyTest());
    }
}

 

 效果和if else是一样的,但是代码变得精简了,后期的扩展也变得简单明了了

如果配合spring使用的话我们可以只需将Bean注入到IOC容器当中 直接使用getBean将实例代码获取出来这样就可以不用写枚举类型 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/919268.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号