哪种设计模式适合以下场景?

时间:2017-02-06 10:16:37

标签: java oop design-patterns

文件存储包含各种大小的图像。客户必须要求获取所需尺寸的图像。客户端无需提供图像宽度和高度,而是引入了模式,它决定了图像的宽度和高度,如下所示。

用于读取文件内容的Servlet如下

private static final int DOWNLOAD_MODE = 0;
private static final int VIEW_MODE = 1;
private static final int COLUMN_MODE = 2;
private static final int ROW_MODE = 3;

private Map<String, Integer> modeHash;
{
    modeHash = new HashMap<String, Integer>();
    modeHash.put(“download”, DOWNLOAD_MODE);
    modeHash.put(“view”, VIEW_MODE);
    modeHash.put(“column”, COLUMN_MODE);
    modeHash.put(“row”, ROW_MODE);
}

public void doGet(HttpServletRequest req, HttpServletResponse res)  throws ServletException, IOException {

String author = “”;
String url = req.getRequestURI();
int action_mode = 0;

String fileid = request.getAttribute(“File_id”);
String downloadMode = request.getAttribute(“download-mode”);
action_mode = modeHash.get(downloadMode);

String userAgent = req.getHeader(“user-agent”);

try {

    //Collecting file meta details and setting it in a hash map (hMap)

    if(hMap != null) {
        readFileContent(action_mode, hMap, req, res);
    } else {
        //Print some error
    }
} catch(Exception ex) {
}
}

//And other util methods like

private HashMap<String, String> getParamsFromURL(int action_mode, HashMap<String, String> hMap,HttpServletRequest req, HttpServletResponse res) {

    //Populating file meta info to hash map
}

private HashMap<String, String> getFileMeta(HashMap<String,String> hMap, String file_information, int action_mode) throws Exception{

//Does some file permission check here and populates meta in cache
}

private JSONObject getSizeDetails(int mode, String fileid){

//Get image dimensions
}

private void getReadStream(int action_mode, HashMap <String,String> hmap,HttpServletRequest req, HttpServletResponse res) throws Exception{

//File read
}

在代码中此处和那里收集文件元数据使其过于笨拙。建议一些更好的方法来组织代码以更顺畅的方式处理以下操作

  1. 文件级别权限检查
  2. 收集文件元数据
  3. 阅读文件流内容
  4. 在这种情况下哪一个适合

    i]天气创建对象并利用对象状态变量而不是散列或在hashMap中收集文件元数据最好的一个?

1 个答案:

答案 0 :(得分:0)

所以你的问题是你想要干净利落地,逻辑地组织一个文件的(meta)信息收集,对吗? 如何DecoratorFilterInputStream延伸,用包含键值对列表的MetaAwareStream和返回表示元数据的Map<K,V>的getter来装饰它。然后用SizedStreamFileMetaStreamPOSIXPermissionStream以及填充MetaAwareStream键/值的任何其他内容装饰它。