一、使用策略模式
1、JS 实例实操
- 优化前
js
复制代码
function calculateBonus(employeeType, salary) {
if (employeeType === "Manager") {
return salary * 0.2;
} else if (employeeType === "Developer") {
return salary * 0.1;
} else if (employeeType === "Intern") {
return salary * 0.05;
} else {
return 0;
}
}
- 优化后
js
复制代码
const bonusStrategies = {
Manager: (salary) => salary * 0.2,
Developer: (salary) => salary * 0.1,
Intern: (salary) => salary * 0.05,
default: () => 0,
};
function calculateBonus(employeeType, salary) {
const strategy = bonusStrategies[employeeType] || bonusStrategies.default;
return strategy(salary);
}
2、Java 实例实操
- 优化前
java
复制代码
public static double calculateBonus(String employeeType, double salary) {
if ("Manager".equals(employeeType)) {
return salary * 0.2;
} else if ("Developer".equals(employeeType)) {
return salary * 0.1;
} else if ("Intern".equals(employeeType)) {
return salary * 0.05;
} else {
return 0;
}
}
- 优化后
java
复制代码
interface BonusStrategy {
double calculate(double salary);
}
public class BonusCalculator {
private Map<String, BonusStrategy> strategies;
public BonusCalculator() {
strategies = new HashMap<>();
strategies.put("Manager", salary -> salary * 0.2);
strategies.put("Developer", salary -> salary * 0.1);
strategies.put("Intern", salary -> salary * 0.05);
}
public double calculateBonus(String employeeType, double salary) {
BonusStrategy strategy = strategies.get(employeeType);
return strategy != null ? strategy.calculate(salary) : 0;
}
}
二、使用映射表
1、JS 实例实操
- 优化前
js
复制代码
function getDayName(dayNumber) {
if (dayNumber === 0) return "Sunday";
else if (dayNumber === 1) return "Monday";
else if (dayNumber === 2) return "Tuesday";
else if (dayNumber === 3) return "Wednesday";
else if (dayNumber === 4) return "Thursday";
else if (dayNumber === 5) return "Friday";
else if (dayNumber === 6) return "Saturday";
else return "Invalid day";
}
- 优化后
js
复制代码
const dayNames = {
0: "Sunday",
1: "Monday",
2: "Tuesday",
3: "Wednesday",
4: "Thursday",
5: "Friday",
6: "Saturday",
};
function getDayName(dayNumber) {
return dayNames[dayNumber] || "Invalid day";
}
2、Java 实例实操
- 优化前
java
复制代码
public static String getDayName(int dayNumber) {
switch (dayNumber) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
default:
return "Invalid day";
}
}
- 优化后
java
复制代码
public class DayUtils {
private static final Map<Integer, String> DAY_NAMES = Map.of(
0, "Sunday",
1, "Monday",
2, "Tuesday",
3, "Wednesday",
4, "Thursday",
5, "Friday",
6, "Saturday"
);
public static String getDayName(int dayNumber) {
return DAY_NAMES.getOrDefault(dayNumber, "Invalid day");
}
}
三、使用枚举
- 优化前
java
复制代码
public static String getStatusDescription(int statusCode) {
if (statusCode == 200) {
return "OK";
} else if (statusCode == 404) {
return "Not Found";
} else if (statusCode == 500) {
return "Internal Server Error";
} else {
return "Unknown Status";
}
}
- 优化后
java
复制代码
public enum HttpStatus {
OK(200, "OK"),
NOT_FOUND(404, "Not Found"),
INTERNAL_ERROR(500, "Internal Server Error");
private final int code;
private final String description;
HttpStatus(int code, String description) {
this.code = code;
this.description = description;
}
public static String getDescription(int code) {
for (HttpStatus status : values()) {
if (status.code == code) {
return status.description;
}
}
return "Unknown Status";
}
}
String description = HttpStatus.getDescription(statusCode);
四、使用函数式编程
1、JS 实例实操
js
复制代码
const conditions = [
{ test: (x) => x < 0, result: "负值" },
{ test: (x) => x === 0, result: "零" },
{ test: (x) => x > 0, result: "正值" },
];
function evaluate(x) {
return conditions.find((cond) => cond.test(x)).result;
}
2、Java 实例实操
java
复制代码
interface Condition {
boolean test(int x);
String getResult();
}
public static String evaluate(int x) {
List<Condition> conditions = Arrays.asList(
new Condition() {
@Override
public boolean test(int x) {
return x < 0;
}
@Override
public String getResult() {
return "负值";
}
},
new Condition() {
@Override
public boolean test(int x) {
return x == 0;
}
@Override
public String getResult() {
return "零";
}
},
new Condition() {
@Override
public boolean test(int x) {
return x > 0;
}
@Override
public String getResult() {
return "正值";
}
}
);
Optional<Condition> matched = conditions.stream()
.filter(cond -> cond.test(x))
.findFirst();
return matched.isPresent() ? matched.get().getResult() : "未知";
}