繁体   English   中英

哪种设计模式适合以下情况?

[英]Which Design Pattern would suit for the following scenario?

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

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. 读取文件流内容

在这种情况下哪一个适合

i]天气创建对象并利用对象状态变量而不是哈希或在hashMap中收集文件元详细信息是最佳选择吗?

因此,您的问题是您想要干净,逻辑地组织文件的(元)信息收集,对吗?
FilterInputStream扩展的Decorator怎么样,用MetaAwareStream对其进行装饰, MetaAwareStream包含一个键-值对列表和一个getter,该getter返回表示元数据的Map<K,V> 然后用SizedStreamFileMetaStreamPOSIXPermissionStream以及填充MetaAwareStream键/值的其他任何东西来装饰它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM