很多时候我们会在代码中使用很笨矬的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));
}
}
网友评论
2在其他语言里甚至是不允许同名方法。(当然我是javaer)
3大型工程要么写getAchievement_A () getAchievement_B()
4当然普遍java大型工程还是单接口。多实现IDE点啊点
5哲学VS工程