方法的重点职责
Focussed Responsibility of a Method
我读过几本 OOP 书籍,其中强调了定义 类 的规则,这些规则集中了一组职责。在我工作过的许多应用程序中,大多数时候都遵守了这一点。然而,我看到很多情况下,很多逻辑被转储到一个方法中,使得难以理解/单元测试。定义方法时需要遵循哪些最佳实践?
普遍的共识是,决定方法应该做什么的最佳方法是遵循 Do One Thing 哲学。
Each method should do one thing and one thing only.
作为示例,让我们看一下以下方法:
public static void printDetails() {
System.out.println("Current time: " + LocalTime.now());
System.out.println("Current date: " + LocalDate.now());
System.out.println("Available processors: " + Runtime.getRuntime().availableProcessors());
System.out.println("Max memory: " + Runtime.getRuntime().maxMemory());
System.out.println("Free memory: " + Runtime.getRuntime().freeMemory());
}
经过一些重构后,我们有了更具可读性和可维护性的版本:
public static void printDetails() {
printDateTimeDetails();
printProcessorDetails();
printMemoryDetails();
}
更容易阅读。更重要的是 - 它更容易测试。
当您阅读此方法的主体时,您会清楚地了解它的用途。如果您需要更多详细信息,只需更深入地查看代码并查看每个方法的作用。
public static void printProcessorDetails() {
System.out.println("Available processors: " + getAvailableProcessors());
}
public static int getAvailableProcessors() {
return Runtime.getRuntime().availableProcessors();
}
public static void printMemoryDetails() {
System.out.println("Max memory: " + getMaxMemory());
System.out.println("Free memory: " + getFreeMemory());
}
public static long getFreeMemory() {
return Runtime.getRuntime().freeMemory();
}
public static long getMaxMemory() {
return Runtime.getRuntime().maxMemory();
}
private static void printDateTimeDetails() {
System.out.println("Current time: " + LocalTime.now());
System.out.println("Current date: " + LocalDate.now());
}
而且这样的代码是可重用的。
The Boy Scouts have a rule: "Always leave the campground cleaner than you found it." Actually the original form of that rule, written by Robert Stephenson Smyth Baden-Powell, the father of scouting, was "Try and leave this world a little better than you found it."
当然,这一切都是我的看法。
我读过几本 OOP 书籍,其中强调了定义 类 的规则,这些规则集中了一组职责。在我工作过的许多应用程序中,大多数时候都遵守了这一点。然而,我看到很多情况下,很多逻辑被转储到一个方法中,使得难以理解/单元测试。定义方法时需要遵循哪些最佳实践?
普遍的共识是,决定方法应该做什么的最佳方法是遵循 Do One Thing 哲学。
Each method should do one thing and one thing only.
作为示例,让我们看一下以下方法:
public static void printDetails() {
System.out.println("Current time: " + LocalTime.now());
System.out.println("Current date: " + LocalDate.now());
System.out.println("Available processors: " + Runtime.getRuntime().availableProcessors());
System.out.println("Max memory: " + Runtime.getRuntime().maxMemory());
System.out.println("Free memory: " + Runtime.getRuntime().freeMemory());
}
经过一些重构后,我们有了更具可读性和可维护性的版本:
public static void printDetails() {
printDateTimeDetails();
printProcessorDetails();
printMemoryDetails();
}
更容易阅读。更重要的是 - 它更容易测试。 当您阅读此方法的主体时,您会清楚地了解它的用途。如果您需要更多详细信息,只需更深入地查看代码并查看每个方法的作用。
public static void printProcessorDetails() {
System.out.println("Available processors: " + getAvailableProcessors());
}
public static int getAvailableProcessors() {
return Runtime.getRuntime().availableProcessors();
}
public static void printMemoryDetails() {
System.out.println("Max memory: " + getMaxMemory());
System.out.println("Free memory: " + getFreeMemory());
}
public static long getFreeMemory() {
return Runtime.getRuntime().freeMemory();
}
public static long getMaxMemory() {
return Runtime.getRuntime().maxMemory();
}
private static void printDateTimeDetails() {
System.out.println("Current time: " + LocalTime.now());
System.out.println("Current date: " + LocalDate.now());
}
而且这样的代码是可重用的。
The Boy Scouts have a rule: "Always leave the campground cleaner than you found it." Actually the original form of that rule, written by Robert Stephenson Smyth Baden-Powell, the father of scouting, was "Try and leave this world a little better than you found it."
当然,这一切都是我的看法。