vendredi 25 août 2023

How to loosen the coupling for an implementation of a map maker?

I want to create a simple dungeon crawler game in Java. The Main function can take an argument "-t" that says that the program uses a file to build the maps and the UI is characters printed to the application screen (and not using JavaFX as an example). I want to make it so I can easily add other ways to make the map (like with a graphic mapmaker) and make the UI use some images, not text. the mapmaking using the file only needs the string path of the file. but other ways of making a map might need other parameters so I can't use an abstract class with the getMap() function that will be implemented in the concrete mapMaking class. I researched and I think this might be a case for dependency injection although I haven't used this design before and can't tell how to implement it here:

public class Main {

    public static void main(String[] args) throws Exception {

        Map map;
        MapGUI mapGUI;
        MapMaker fileMaker;

        if(args[0].compareToIgnoreCase("-t") == 0) {

            mapMaker = new FileMapMaker();
            mapGUI = new MapTextGUI();

        }//other cases will be added in the future...

        map = getMap(mapMaker);

        mapGUI.print(map);
    }
        //might need different parameters here
    private static Map getMap(MapMaker mapMaker) {/*how to inject  here the relevant parameters for each case of map maker  */
        return null;
    }
}

Aucun commentaire:

Enregistrer un commentaire