0

私はJavaを独学しようとしているので、これが正しいかどうかはわかりません。メイン メソッドを含むクラスがあり、このクラス内には、java.util.Properties を使用してユーザー設定にアクセスする必要があるいくつかのサブクラスがあります。プロパティ オブジェクトを機能させるには、すべてのサブクラスでプロパティ オブジェクトを作成する必要があります。configFilePath を使用してオブジェクトを参照することはできません。null でなければなりません。親クラス内でこのパブリック オブジェクトを作成できるかどうか疑問に思っているので、そのすべてのサブクラスで作成する必要はありませんか? これが私のコードです。動作しますが、これを正しく行っているかどうかは本当にわかりません。

public class Frame1 extends JFrame {
    Settings config = new Settings(); //this is the object I want to reference within subclasses

    class Update extends SwingWorker<Integer, Void> {  //first subclass

        @Override
        protected Integer doInBackground() throws Exception {
            Settings config = new Settings(configFilePath);  //yet I have to create the object within every subclass, this time an argument is required.  
            String templateDir = config.getProperty("templatedir");
            String writePath = config.getProperty("outputdir");
            //do some logic code, not required for my question
        }

        @Override
        protected void done() {
            Update2 update2 = new Update2();
            update2.execute(); //start the next subclass which also needs access to Settings(configFilePath)
        }
    } 
}

public class Settings extends JFrame {
    String configFilePath = "C:/path/to/settings.properties";
    Properties properties = new Properties();
    public Settings(String configFilePath) throws IOException {

        this.configFilePath = configFilePath;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(configFilePath);
            properties.load(fis);

        } catch (FileNotFoundException e) {
            setDefaults();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }
}

これを正しく行っているかどうかはわかりませんが、機能しているように見えますが、ユーザー設定にアクセスする必要があるたびに構成オブジェクトを作成する必要があるのはかなり冗長なようです。これが以前に尋ねられていないことを願っています。見つけられなかったので、リンクしてください。

4

2 に答える 2

2

FromUpdateを使用してofFrame1.thisにアクセスできます(は の内部クラスであるため)。thisFrame1UpdateFrame1

次に、アクセスconfigするには、 を使用できますFrame1.this.config

これが実際の例です:

public class PrefixerFactory {
    private String prefix; // Used by Prefixer

    public PrefixerFactory(String prefix) {
        this.prefix = prefix;
    }

    public Prefixer createPrefixer() {
        return new Prefixer();
    }

    public class Prefixer { // Inner class
        public String addPrefix(String value) {
            // Using "prefix" from PrefixerFactory
            return PrefixerFactory.this.prefix + value;
        }
    }

    public static void main(String[] args) {
        Prefixer helloPrefixer = new PrefixerFactory("Hello ").createPrefixer();
        Prefixer goodbyePrefixer = new PrefixerFactory("Good bye ").createPrefixer();

        System.out.println(helloPrefixer.addPrefix("world")); // Hello world
        System.out.println(goodbyePrefixer.addPrefix("world")); // Good bye world
    }
}
于 2013-07-16T18:24:17.337 に答える
2

Setting クラスをSingletonパターンとして作成できます。以下に一例を示します。

public class Settings extends JFrame{
    String configFilePath = "C:/path/to/settings.properties";
    Properties properties = new Properties();

    private static Settings instance;

    public static Settings getInstance(){
       if(instance==null){
           instance = new Setting();
       }
       return instance;
    }

    private Settings() throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(configFilePath);
            properties.load(fis);

        } catch (FileNotFoundException e) {
            setDefaults();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
    }
}

システムの他のクラスでの使用:

Settings.getInstance().getProperty("...");
于 2013-07-16T18:28:27.463 に答える