设计模式:如何为方法添加可选参数?
design pattern: how to add optional params for methods?
我有一个实用程序 class:
public class ImageCompressionUtil {
public static void compressor(InputStream image, OutputStream outputStream, double quality,
String outputFormat) throws IOException {
Thumbnails.of(image).scale(1).outputQuality(quality).outputFormat(outputFormat).
toOutputStream(outputStream);
}
public static void compressor(byte[] image, OutputStream outputStream, double quality,
String outputFormat) throws IOException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(image);
Thumbnails.of(inputStream).scale(1).outputQuality(quality).
outputFormat(outputFormat).toOutputStream(outputStream);
}
public static byte[] compressor(InputStream image, double quality, String outputFormat)
throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(image).scale(1).outputQuality(quality).
outputFormat(outputFormat).toOutputStream(outputStream);
return outputStream.toByteArray();
}
public static byte[] compressor(byte[] image, double quality, String outputFormat) throws IOException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(image);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(inputStream).scale(1).outputQuality(quality).
outputFormat(outputFormat).toOutputStream(outputStream);
return outputStream.toByteArray();
}
}
在这些方法中,我们可以看到一些重复的参数,例如outputStream
、quality
、outputFormat
。
如何重写此 class 以便我可以像这样的模式使用此 class:
// no outputstream/quality/outputFormat specified, default values are used;
ImageCompressionUtil.compressor(image);
// specifiy outputstream or quality or outputFormat
ImageCompressionUtil.compressor(image, outputstream);
ImageCompressionUtil.compressor(image).quality(quality);
ImageCompressionUtil.compressor(image).outputFormat(outputFormat);
// both quality and outputFormat are specified
ImageCompressionUtil.compressor(image).quality(quality).outputFormat(outputFormat);
...
是的:它看起来像构建器模式。
您只需要两个 compressor
方法来 return Compressor
class(构建器)的一个实例:
public class ImageCompressionUtil {
public Compressor compressor(InputStream image) {
return new Compressor(image);
}
public Compressor compressor(byte[] image) {
return new Compressor(new ByteArrayInputStream(image));
}
}
Compressor
class 看起来像这样:
public class Compressor {
private final Thumbnail thumbnail;
public Compressor(InputStream image) {
this.thumbnail = Thumbnail.of(image).scale(1);
}
public Compressor quality(double quality) {
thumbnail.outputQuality(quality);
return this;
}
public Compressor outputFormat(String format) {
thumbnail.outputFormat(format);
return this;
}
public void writeTo(OutputStream outputStream) {
thumbnail.toOutputStream(outputStream);
}
public byte[] toBytes() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
writeTo(out);
return out.toByteArray();
}
}
因此您首先要创建一个压缩器,然后设置参数,然后将结果写入 OutputStream
或将其作为字节数组获取。
有一件事不清楚 class 应该关闭 InputStream
。
请注意,Compressor
class 与我所说的 Thumbnail
class 没有太大区别(return 由 Thumbnails.of()
), 你可以直接 return Thumbnails.of(image).scale(1)
的结果而不是创建另一个 class.
我有一个实用程序 class:
public class ImageCompressionUtil {
public static void compressor(InputStream image, OutputStream outputStream, double quality,
String outputFormat) throws IOException {
Thumbnails.of(image).scale(1).outputQuality(quality).outputFormat(outputFormat).
toOutputStream(outputStream);
}
public static void compressor(byte[] image, OutputStream outputStream, double quality,
String outputFormat) throws IOException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(image);
Thumbnails.of(inputStream).scale(1).outputQuality(quality).
outputFormat(outputFormat).toOutputStream(outputStream);
}
public static byte[] compressor(InputStream image, double quality, String outputFormat)
throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(image).scale(1).outputQuality(quality).
outputFormat(outputFormat).toOutputStream(outputStream);
return outputStream.toByteArray();
}
public static byte[] compressor(byte[] image, double quality, String outputFormat) throws IOException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(image);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Thumbnails.of(inputStream).scale(1).outputQuality(quality).
outputFormat(outputFormat).toOutputStream(outputStream);
return outputStream.toByteArray();
}
}
在这些方法中,我们可以看到一些重复的参数,例如outputStream
、quality
、outputFormat
。
如何重写此 class 以便我可以像这样的模式使用此 class:
// no outputstream/quality/outputFormat specified, default values are used;
ImageCompressionUtil.compressor(image);
// specifiy outputstream or quality or outputFormat
ImageCompressionUtil.compressor(image, outputstream);
ImageCompressionUtil.compressor(image).quality(quality);
ImageCompressionUtil.compressor(image).outputFormat(outputFormat);
// both quality and outputFormat are specified
ImageCompressionUtil.compressor(image).quality(quality).outputFormat(outputFormat);
...
是的:它看起来像构建器模式。
您只需要两个 compressor
方法来 return Compressor
class(构建器)的一个实例:
public class ImageCompressionUtil {
public Compressor compressor(InputStream image) {
return new Compressor(image);
}
public Compressor compressor(byte[] image) {
return new Compressor(new ByteArrayInputStream(image));
}
}
Compressor
class 看起来像这样:
public class Compressor {
private final Thumbnail thumbnail;
public Compressor(InputStream image) {
this.thumbnail = Thumbnail.of(image).scale(1);
}
public Compressor quality(double quality) {
thumbnail.outputQuality(quality);
return this;
}
public Compressor outputFormat(String format) {
thumbnail.outputFormat(format);
return this;
}
public void writeTo(OutputStream outputStream) {
thumbnail.toOutputStream(outputStream);
}
public byte[] toBytes() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
writeTo(out);
return out.toByteArray();
}
}
因此您首先要创建一个压缩器,然后设置参数,然后将结果写入 OutputStream
或将其作为字节数组获取。
有一件事不清楚 class 应该关闭 InputStream
。
请注意,Compressor
class 与我所说的 Thumbnail
class 没有太大区别(return 由 Thumbnails.of()
), 你可以直接 return Thumbnails.of(image).scale(1)
的结果而不是创建另一个 class.