lundi 6 février 2017

Which Design Pattern would suit for the following scenario?

A file store contains images of various sizes. Client has to request to get the image of required dimension. The Client need not provide image width and height instead mode has been introduced which determines the width and height of the image as follows.

The Servlet to read the file content is as follows

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
}

Collecting file meta details here and there within the code makes it too clumsy. Suggest some better way to organise the code to handle the following actions in a smoother way

  1. file level permission checks
  2. collecting file meta details
  3. reading file stream content

Which one would suit in this case

i] Weather creating object and making use of object state variables instead of hash or collecting file meta details in the hashMap a best one?

Aucun commentaire:

Enregistrer un commentaire