与一般或特殊方法的接口?

时间:2018-01-17 10:12:14

标签: java

目前我需要保存四种类型的文件,电影图片,电影海报,电影预告片和用户头像。我想在图片文件夹中放置照片和海报,我想在头像文件夹中放置头像,在预告片文件夹中放置预告片。

我当前的界面如下所示

public interface StorageService {
    String save (
            final File file,
            final String contentType
    ) throws IOException;
...
}

自定义save方法以放置不同的文件我想知道使用哪种方式。

第一种方法是添加新参数String folder

 public interface StorageService {
    String save (
            final File file,
            final String contentType,
            final String folder
    ) throws IOException;
...
}

并在变量文件夹中,输入要添加文件的文件夹的名称。

第二种方法是创建三个单独的方法

public interface StorageService {
    String saveImage (
            final File file,
            final String contentType
    ) throws IOException;
    String saveAvatar (
            final File file,
            final String contentType
    ) throws IOException;
    String saveTrailer (
            final File file,
            final String contentType
    ) throws IOException;
...
}
你怎么看?哪个想法更好?

1 个答案:

答案 0 :(得分:0)

你检查了战略模式吗?您执行的操作与此相同,但策略因某些条件而异。所以这个模式应该是一个完美的契合