简体   繁体   English

Java从内部类单例访问外部类变量

[英]Java Accessing outer class variables from an inner class singleton

I have an interface ( Node ) that is implemented by two classes: White , which I want to be a singleton, and Black , not a singleton. 我有一个由两个类实现的接口( Node ): White ,我想成为一个单例; Black ,而不是一个单例。 The problem is I want these classes to be enclosed by an outer class ( Outer ), which has a field of its own ( outerObject ). 问题是我希望这些类被一个外部类( Outer )包围,该类具有自己的字段( outerObject )。 White should be a singleton, which means it should be static. White应该是单例,这意味着它应该是静态的。 But Black needs to be able to access the outer class fields, which means it cannot be static. 但是Black需要能够访问外部类字段,这意味着它不能是静态的。

Here, if White is not static, then it's not really a singleton: 在这里,如果White不是静态的,那么它实际上不是单例的:

class Outer {
    interface Node {
        Node foo();
    }

    // I would like the White class to be a singleton class
    class White implements Node {
        Node foo() {
            return new Black();
        }
    }

    class Black implements Node {
        Node foo() {
            outerObject.doSomething();
            return this;
        }
    }

    Object outerObject;
}

But if I make White a static singleton, then it cannot instantiate a new Black without an enclosing Outer : 但是,如果我将White设为静态单例,那么在没有封闭的Outer情况下无法实例化新的Black

    enum White implements Node {
        INSTANCE;

        Node foo() {
            return new Black(); // This doesn't work because Black
                                // needs an enclosing outer class.
        }
    }

And if I were to make Black a static class, then it cannot access the field ( outerObject ) in the outer class: 而且,如果我将Black设为静态类,则它无法访问外部类中的字段( outerObject ):

    static class Black implements Node {
        Node foo() {
            outerObject.doSomething(); // can't access non-static field
            return this;
        }
    }

A simpler representation which captures the same problem is simply trying to access outerObject from a singleton White : 捕获相同问题的更简单表示形式就是尝试从单例White访问outerObject

class Outer {
    enum White {
        INSTANCE;

        Node foo() {
            outerObject.doSomething();
            return this;
        }
    }

    Object outerObject;
}

I'd have to make White non-static to be able to access outerObject , but it should be static in order to be singleton. 我必须使White非静态的,以便能够访问outerObject ,但是为了成为单例,它应该是静态的。

Does anyone know if there is a solution to this? 有谁知道是否有解决方案? If it comes down to it, I could try to make a pseudo-singleton inner class (in the sense that each instance of the outer class can only have one instance of that inner class). 如果涉及到这一点,我可以尝试创建一个伪单内部类(在某种意义上,外部类的每个实例只能具有该内部类的一个实例)。 Or just ditch the singleton pattern and manually declare only one White . 或者只是放弃单例模式并手动声明一个White

You can specify on what instanse of Outer you want to create new Black. 您可以指定要创建新Black的外部实例。 But probably it is not what you want. 但这可能不是您想要的。

    enum White implements Node {
        INSTANCE;

        public Node foo() {
            return new Outer().new Black(); // This will work
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM