我将创建一个» ImageProcesssor «(或任何适合您项目的名称)和一个配置对象ProcessConfiguration,其中包含所有必要的参数。
ImageProcessor p = new ImageProcessor();
ProcessConfiguration config = new processConfiguration().setTranslateX(100)
.setTranslateY(100)
.setRotationAngle(45);
p.process(image, config);
在图像处理器内部,将整个过程封装在一个方法后面 process()
public class ImageProcessor {
public Image process(Image i, ProcessConfiguration c){
Image processedImage=i.getCopy();
shift(processedImage, c);
rotate(processedImage, c);
return processedImage;
}
private void rotate(Image i, ProcessConfiguration c) {
//rotate
}
private void shift(Image i, ProcessConfiguration c) {
//shift
}
}
该方法调用正确的顺序变革方法shift()
,rotate()
。每个方法都从传递的ProcessConfiguration中获取适当的参数。
public class ProcessConfiguration {
private int translateX;
private int rotationAngle;
public int getRotationAngle() {
return rotationAngle;
}
public ProcessConfiguration setRotationAngle(int rotationAngle){
this.rotationAngle=rotationAngle;
return this;
}
public int getTranslateY() {
return translateY;
}
public ProcessConfiguration setTranslateY(int translateY) {
this.translateY = translateY;
return this;
}
public int getTranslateX() {
return translateX;
}
public ProcessConfiguration setTranslateX(int translateX) {
this.translateX = translateX;
return this;
}
private int translateY;
}
我使用了流体接口
public ProcessConfiguration setRotationAngle(int rotationAngle){
this.rotationAngle=rotationAngle;
return this;
}
允许进行漂亮的初始化(如上所示)。
明显的优势是将必要的参数封装在一个对象中。您的方法签名变得可读:
private void shift(Image i, ProcessConfiguration c)
它是关于移动的图像和详细参数以某种方式配置。
或者,您可以创建一个ProcessingPipeline:
public class ProcessingPipeLine {
Image i;
public ProcessingPipeLine(Image i){
this.i=i;
};
public ProcessingPipeLine shift(Coordinates c){
shiftImage(c);
return this;
}
public ProcessingPipeLine rotate(int a){
rotateImage(a);
return this;
}
public Image getResultingImage(){
return i;
}
private void rotateImage(int angle) {
//shift
}
private void shiftImage(Coordinates c) {
//shift
}
}
对方法的方法调用processImage
将实例化这样的管道,并使执行的顺序和顺序透明:shift,rotation
public Image processImage(Image i, ProcessConfiguration c){
Image processedImage=i.getCopy();
processedImage=new ProcessingPipeLine(processedImage)
.shift(c.getCoordinates())
.rotate(c.getRotationAngle())
.getResultingImage();
return processedImage;
}