Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java invoker

Name: Anonymous 2012-12-01 19:13

In Java, is it possible to get the invoker of a class? I am trying to create two classes simultaneously but I want them to reference each other, preferably upon creation(constructors) if possible. I know I can sort of do it by returning the stack trace, but this doesn't exactly seem... proper. Any ideas /prog/?

Name: Anonymous 2012-12-01 19:58


class Controller {
    private View _view;

    public Controller(ViewFactory vf) {
        _view = vf.build(this);
    }

    public getView() {
        return _view;
    }
}

class View {
    private Controller _controller;

    public View(Controller controller) {
        _controller = controller;
    }

    public getController() {
        return _controller;
    }
}

class ViewFactory {
    private String name;

    public ViewFactory(String name) {
        _name = name;
    }

    public View build(Controller controller) {
        // use reflection, not sure if this is right
        return Class.forName(name).newInstance(controller);
    }
}

// usage
ViewFactory vf = new ViewFactory("View");
Controller c = new Controller(vf);
View v = c.getView();


the other way to build a circular reference is to use setter injection

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List