design patterns - Java: Take anonymous class outside -
i have situation presented below:
class c1 { public static void main(string[] args) { object o = // new object of class delegate(new c2() { // c2 abstract class or interface public void delegatelogic() { object my_o = o; // refrences main's local variable // huge method body // object o local main required here } }); } private void delegate(c2 c2) { // method body } }
the body of delegatelogic() turning out big. code maintainability :
- i want create concrete class c2 , keep outside, while still having way use object o in main method.
- also, instance of c2 supposed serializable , unfortunately object o not serializable. so, not want create member object in c2 passing object o in constructor.
any ideas?
if want c2 able serialized, declare o transient. however, need accept fact o null if got serialized/deserialized unless manage serialize manually somehow.
public class x extends c2 { private transient object o; public x(object o) { this.o = o; } public void delegatelogic() { object my_o = o; // refrences main's local variable // huge method body // object o local main required here } }
Comments
Post a Comment