很多時候我們會在代碼中使用很笨矬的if/else、switch語句离唐,代碼閱讀起來相當?shù)某粤ΑS袥]有一種方式可以替換呢问窃。今天就讓我們看看強大策略模式如何解決這一問題吧亥鬓。
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 "學生成績?yōu)?A";
} else if (this.achievement == Achievement.B) {
return "學生成績?yōu)?B";
} else {
return "學生成績?yōu)?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這樣的語句域庇,下面我們先使用策略模式來替換嵌戈。我們先來看看類結(jié)構(gòu)(UML)。
成績接口(Achievement)
package com.zhanghf.noifelse.strategy;
/**
* Created by Administrator on 2017/4/9.
*/
public interface Achievement {
String getAchievement();
}
優(yōu)秀成績類(ExcellentAchievement)
package com.zhanghf.noifelse.strategy;
/**
* Created by Administrator on 2017/4/9.
*/
public class ExcellentAchievement implements Achievement {
public String getAchievement() {
return "學生成績?yōu)?A";
}
}
良好成績類(GoodAchievement)
package com.zhanghf.noifelse.strategy;
/**
* Created by Administrator on 2017/4/9.
*/
public class GoodAchievement implements Achievement {
public String getAchievement() {
return "學生成績?yōu)?B";
}
}
及格成績類(AverageAchievement)
package com.zhanghf.noifelse.strategy;
/**
* Created by Administrator on 2017/4/9.
*/
public class AverageAchievement implements Achievement {
public String getAchievement() {
return "學生成績?yōu)?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());
}
}
下面使用多態(tài)方式:
學生類(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 "學生成績?yōu)?C";
}
}
良好成績類(GoodAchievement)
package com.zhanghf.noifelse.polymorphic;
/**
* Created by Administrator on 2017/4/9.
*/
public class GoodAchievement{
public String getAchievement() {
return "學生成績?yōu)?B";
}
}
優(yōu)秀成績類(ExcellentAchievement)
package com.zhanghf.noifelse.polymorphic;
/**
* Created by Administrator on 2017/4/9.
*/
public class ExcellentAchievement {
public String getAchievement() {
return "學生成績?yōu)?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));
}
}