jeudi 14 mai 2015

Java: Take anonymous class outside

I have a situation presented below:

Class C1 {

    public static void main(String[] args) {
        Object o = // new object of some class
        delegate(new C2() {             // C2 is an abstract class or interface
            public void delegateLogic() {
                Object my_o = o;  // refrences main's local variable
                // huge method body
                // object o local to main is required here  
            }
        });
    }

    private void delegate(C2 c2) {
        // method body
    }
}

The body of delegateLogic() is turning out to be very big. For code maintainability :

  1. I want to create a concrete class from C2 and keep it outside, while still having a way to use object o in the main method.
  2. Also, instance of C2 are supposed to be serializable and unfortunately object o is not serializable. So, I do not want to create a member object in C2 by passing a in the constructor.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire