美文网首页iOS开发设计模式
使用策略模式和多态去掉if/else、switch

使用策略模式和多态去掉if/else、switch

作者: brightranger | 来源:发表于2017-04-09 11:13 被阅读1707次

    很多时候我们会在代码中使用很笨矬的if/else、switch语句,代码阅读起来相当的吃力。有没有一种方式可以替换呢。今天就让我们看看强大策略模式如何解决这一问题吧。

    if/else示例

    学生类(student)

    package com.zhanghf.noifelse;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Student {
        private Achievement achievement;
    
        /**
         * 获取学生成绩
         *
         * @return 成绩
         */
        public String getAchievement() {
            if (this.achievement == Achievement.A) {
                return "学生成绩为:A";
            } else if (this.achievement == Achievement.B) {
                return "学生成绩为:B";
            } else {
                return "学生成绩为:C";
            }
        }
    }
    
    

    成绩枚举类(Achievement)

    package com.zhanghf.noifelse;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public enum Achievement {
        A,B
    }
    
    

    方法测试类(Application)

    package com.zhanghf.noifelse;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public class Application {
    
        public static void main(String[] args) {
            Student student = new Student(Achievement.B);
            System.out.println(student.getAchievement());
        }
    
    }
    
    

    可以看到在getAchievement方法中,使用了很多if/else if /else这样的语句,下面我们先使用策略模式来替换。我们先来看看类结构(UML)。

    策略模式.png

    成绩接口(Achievement)

    package com.zhanghf.noifelse.strategy;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public interface Achievement {
        String getAchievement();
    }
    

    优秀成绩类(ExcellentAchievement)

    package com.zhanghf.noifelse.strategy;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public class ExcellentAchievement implements Achievement {
    
        public String getAchievement() {
            return "学生成绩为:A";
        }
    }
    

    良好成绩类(GoodAchievement)

    package com.zhanghf.noifelse.strategy;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public class GoodAchievement implements Achievement {
    
        public String getAchievement() {
            return "学生成绩为:B";
        }
    }
    

    及格成绩类(AverageAchievement)

    package com.zhanghf.noifelse.strategy;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public class AverageAchievement implements Achievement {
    
        public String getAchievement() {
            return "学生成绩为:C";
        }
    }
    

    学生类(Student)

    package com.zhanghf.noifelse.strategy;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Student {
        private Achievement achievement;
    
        public String getAchievement() {
            return this.achievement.getAchievement();
        }
    }
    

    代码测试类(Application)

    package com.zhanghf.noifelse.strategy;
    
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public class Application {
        public static void main(String[] args) {
            Student student = new Student(new AverageAchievement());
            System.out.println(student.getAchievement());
        }
    }
    

    下面使用多态方式:

    学生类(student)

    package com.zhanghf.noifelse.polymorphic;
    
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    @Data
    @NoArgsConstructor
    public class Student {
    
        public String getAchievement(AverageAchievement averageAchievement) {
            return averageAchievement.getAchievement();
        }
    
        public String getAchievement(ExcellentAchievement excellentAchievement) {
            return excellentAchievement.getAchievement();
        }
    
        public String getAchievement(GoodAchievement goodAchievement) {
            return goodAchievement.getAchievement();
        }
    
    }
    

    及格成绩类(AverageAchievement)

    package com.zhanghf.noifelse.polymorphic;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public class AverageAchievement{
    
        public String getAchievement() {
            return "学生成绩为:C";
        }
    }
    

    良好成绩类(GoodAchievement)

    package com.zhanghf.noifelse.polymorphic;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public class GoodAchievement{
    
        public String getAchievement() {
            return "学生成绩为:B";
        }
    }
    

    优秀成绩类(ExcellentAchievement)

    package com.zhanghf.noifelse.polymorphic;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public class ExcellentAchievement {
    
        public String getAchievement() {
            return "学生成绩为:A";
        }
    }
    

    代码测试类(Application)

    package com.zhanghf.noifelse.polymorphic;
    
    /**
     * Created by Administrator on 2017/4/9.
     */
    public class Application {
        public static void main(String[] args) {
            Student student = new Student();
            AverageAchievement averageAchievement = new AverageAchievement();
            System.out.println(student.getAchievement(averageAchievement));
        }
    }
    

    相关文章

      网友评论

      • 10d3642f94b1:实用原则就是if/else switch的量有多大,简单几条可以不用
      • 仰泳鱼:1为了消除if else,写了三 getAchievement() 方法。(参数不一样)
        2在其他语言里甚至是不允许同名方法。(当然我是javaer)
        3大型工程要么写getAchievement_A () getAchievement_B()
        4当然普遍java大型工程还是单接口。多实现IDE点啊点
        5哲学VS工程
      • xiao雨痕:if/else、switch 阅读起来倒没有想象中吃力,这样更改会不会属于过度设计?
        pseudo_niaonao:我在想在底层还是要判断的,前面传来个type,你还是要根据type确定使用那个策略的实现类,不是吗
        24dd5635e06a:策略模式主要是更好的扩展性,

      本文标题:使用策略模式和多态去掉if/else、switch

      本文链接:https://www.haomeiwen.com/subject/exhdattx.html