[英]Reducing number of similar methods
I have a method which does something from point a to point b (it iterates by incrementation) or from b to a (it iterates by decrementation) through an array. 我有一个方法可以通过数组从点a到点b(通过递增迭代)或从b到a(通过递减迭代)执行某项操作。
These two methods have to be a to b and b to a because I do not know at the beginning, where will I end. 这两种方法必须是a到b和b到a,因为我一开始不知道在哪里结束。 In my implementation, they vary only in one line. 在我的实现中,它们仅在一行中变化。
In more dimensions I still do it only in straight lines. 在更大的维度上,我仍然仅沿直线进行。 Problem expands from two (left, right) to eight (up, up-right, right, down-right etc.), 32 and so on functions. 问题从两个(左,右)扩展到八个(上,右,右,右下等),32个函数。 They also start varying in more lines. 它们也开始在更多行中变化。
What is the best way to deal with the problem? 解决问题的最佳方法是什么?
I have to deal with it in java. 我必须在Java中处理它。
Example in 2 dimensions (three from eight functions to cover every possibility): 二维示例(八个功能中的三个,涵盖所有可能性):
void doSomethingToCellsRIGHT(int x, int y) {
while(condition) {
board[x][y].changeSomething();
x++;
}
}
void doSomethingToCellsLEFT(int x, int y) {
while(condition) {
board[x][y].changeSomething();
x--;
}
}
void doSomethingToCellsUP_LEFT(int x, int y) {
while(condition) {
board[x][y].changeSomething();
y++;
x--;
}
}
add an enum 添加一个枚举
public enum DIRECTION {
Right,
Left,
Up,
Down
}
You could do this and have multiple optional parameters, where you always need at least one direction; 您可以执行此操作,并具有多个可选参数,在这些参数中,您始终需要至少一个方向;
void doSomethingToCells(int x, int y, DIRECTION... directions){
while(condition){
board[x][y].changeSomething();
for(DIRECTION dir:directions){
y+= dir == DIRECTION.Up ? 1 : 0;
y-= dir == DIRECTION.Down ? 1 : 0;
x+= dir == DIRECTION.Right ? 1 : 0;
x-= dir == DIRECTION.Left ? 1 : 0;
}
}
}
and you can call 你可以打电话
doSomethingWithCells( 1,1, Up, Left) . // This would go x-- and y++
doSomethingWithCells( 1,1, Up) // This would go y++
You could even call 你甚至可以打电话
doSomethingWithCells( 1,1, Left, Left) . //This would skip every second cell to the left
Cool, a good question, the codes are quite the same with @Derek but make it more OO: 太酷了,这是一个很好的问题,@ Derek的代码与之完全相同,但使其更加面向对象:
Move
interface 定义Move
界面 public interface Move {
public void move(int x, int y);
}
enum
with method body 用方法体在enum
实现 public enum Movement implements Move {
RIGHT() {
public void move(int x, int y) {
x++;
}
},
LEFT() {
public void move(int x, int y) {
x--;
}
},
UP() {
public void move(int x, int y) {
y++;
}
},
DOWN() {
public void move(int x, int y) {
y--;
}
};
}
public void doSomething(int x, int y, Movement... movements) {
while (condition) {
board[x][y].changeSomething();
for (Movement movement : movements) {
movement.move(x, y);
}
}
}
doSomething(1, 2, Movement.DOWN, Movement.RIGHT);
The answer by Liping could be an improvement, except for that the method Liping的答案可能是一个改进,除了该方法
public void move(int x, int y) {
x++;
}
does nothing at all (incrementing a local variable is a no-op here). 什么都不做(在这里增加局部变量是无操作的)。
Anyway, I'd do it in a shorter and probably faster way: 无论如何,我会以更短且可能更快的方式进行:
public interface Move {
public int dx();
public int dy();
}
@RequiredArgsConstructor
public enum Movement implements Move {
RIGHT(+1, 0),
LEFT(-1, 0),
UP(0, +1),
DOWN(0, -1);
private final dx;
private final dy;
}
The Lombok annotation does exactly what the name says. Lombok批注完全按照名称中的说明进行操作。
The usage should be obvious. 用法应该很明显。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.