I am working on an application that has to read certain data from the screen, and to do so I am using a constants class to specify the position of the data in the screen. For instance, one variable with information would look like this:
//Square of 100x15 px containing the name of the current user
public static final int[] USERNAME = {1157, 700, 100, 15};
//Squares for the five 16x23 numbers common to all players
private static final int[][] COMMON_NUMBERS = {
{426, 230, 16, 23},
{514, 230, 16, 23},
{601, 230, 16, 23},
{688, 230, 16, 23},
{775, 230, 16, 23}};
An example of the use of this class would be:
BufferedImage username = screenshot.getSubimage(Constants.USERNAME[0], Constants.USERNAME[1], Constants.USERNAME[2], Constants.USERNAME[3]);
Up to here everything is fine, but now I need more Constants representing the same thing but for a different screen resolution. I cannot multiply by the ratio of the screen resolutions, as some element's positions are slightly different and sometimes the readings aren't correct.
I was wondering whether there was a nice and clean design pattern for this situation that I can use, as I am not fully convinced with the different options that I have in mind:
- Adding resolution as a new dimension of the variables, for instance the above example would be something like this:
public static final int[][] USERNAME = {{1157, 700, 100, 15},{1100, 650, 100, 15}};
and I will need to send information about which resolution it is from every class using these constants. Also, when adding a few more different resolutions this class would become extremely big.
- Using an interface containing getters for all attributes, for instance:
int[] getUsername();
int[][] getCommonNumbers();
Then, for each resolution I would implement this interface, and I would set the correct implementation with spring whenever my application starts.
- Using an intermediate class instead of an interface, which will contain information about the resolution to use, will contain all methods as the previous interface, and will return the attribute from the correct constants file. This could be done implementing manually all methods or with java reflections, so that one central method could handle all calls. This way whenever I want to use my constants I would call the intermediate class.
I wanted to know what would be a good design for this problem, as I am not fully convinced with any of these solutions. Is there some design pattern for this?
I was reading about the Constant interface, but I am not sure about how to apply it here.
Aucun commentaire:
Enregistrer un commentaire