Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming that responding to a command is not that different from responding to a http request, I think you're going the right path.</p> <p>A commonly used pattern in http applications is to wrap logic of the application into short lived objects that have both parameters from request and some backends injected. Then you instantiate such object and call a simple, parameterless method that does all magic. </p> <p>Maybe scopes could inspire you somehow? Look <a href="http://code.google.com/p/google-guice/wiki/Scopes" rel="nofollow noreferrer">into documentation</a> and <a href="http://code.google.com/p/google-guice/wiki/CustomScopes" rel="nofollow noreferrer">some code examples</a> for read the technical details. In code it looks more less like that. Here's how this might work for your case:</p> <pre><code>class MyRobot { Scope myScope; Injector i; public void doCommand(Command c) { myScope.seed(Key.get(Command.class), i.getInstance(Handler.class).doSomething(); } } class Handler { private final Command c; @Inject public Handler(Command c, Hardware h) { this.c = c; } public boolean doSomething() { h.doCommand(c); // or c.modifyState(h) if you want c to access internals of h } } </code></pre> <p>Some people frown upon this solution, but I've seen this in code relying heavily on Guice in the past in at least two different projects.</p> <p>Granted you'll inject a bit of value objects in the constructors, but if you don't think of them as value objects but rather parameters of the class that change it's behaviour it all makes sense.</p> <p>It is a bit awkward and some people frown upon injecting value objects that way, but I have seen this in the past in projects that relied heavily on Guice for a while and it worked great.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload