Java – get the correct conversion object from HashMap

I am developing an application with multiple systems (in my example, the system is a loadable component in my application that handles specific tasks, such as translation, configuration processing, audio, etc.) These systems share some common methods, so I created an interface:

public interface System {
    public void initialize();
    public void uninitialize();
}

But each system has its own method, which corresponds to the task of system implementation These are two examples of my system

First storage setting:

public class SettingsSystem implements System {
    @Override
    public void initialize() {
        // few lines of code here
    }

    @Override
    public void uninitialize() {
        // few lines of code here
    }

    public Object getSetting(String key) {
        // returns a setting
    }
}

And the language system of translation

public class LanguageSystem implements System {

    @Override
    public void initialize() {
        // few lines of code here
    }

    @Override
    public void uninitialize() {
        // few lines of code here
    }

    public void setLanguage(String language) {
        // sets language
    }

    public boolean load( String name) {
        // loads a file
    }

    public String translate(String key) {
        // returns translation of a key
    }
}

I store system instances in a static HashMap because I want to access them anywhere in my application I created a static method to return any system with a key

public class App {
    // note: System is the System interface I created
    private static Map<String,System> systems = new HashMap<String,System>();

    public static Object getSystem(String key) {
        return systems.get(key);
    }
}

If I call app Getsystem () method, which will return an object So if I call

App.getSystem("settings");

... it will return the correct item as the object The problem is that I can't call the getsetting () method of settingssystem because it tells me that there is no such method

If I do this:

public static System getSystem(String key) {
    return systems.get(key);
}

I can call initialize () or Uninitialize () methods, but still no getsettings () method is available

What I want is when I call app When using the getsystem () method, I want it to return the correct type of object it contains Example:

App.getSystem("settings"); // I want it to return a SettingsSystem object
// So I can do:
String width = App.getSystem("settings").getSetting("width");

App.getSystem("language"); // I want it to return a LanguageSystem object
// So I can do:
String welcome = App.getSystem("language").translate("hello_world");

How is this possible? Thank you in advance

UPDATE

With your help, I decided to declare getter methods for each system:

public SettingsSystem getSettingsSystem() {
    return (SettingsSystem) system.get("settings");
}

Thank you for your advice!

Solution

The type of the expression (including the type returned by the call) is fixed at compile time You can't use the same method in the same object. There are several different return types

You have two options:

>Return the type object and cast it in the caller. > Declare a different getxxxsystem method for each type Getlanguagesystem will return the type languagesystem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>