Java: How to pass in an optional override behavior?

I am working on creating a generalized “driver controller” wrapper. It basically bundles a Joystick with a “profile” that points to what axes do what. However, here’s an example of where my implementation falls short.
Currently, here’s my implementation NOMAD-Base-2020/ at shueja-personal/unitTestingRewrite · shueja-personal/NOMAD-Base-2020 · GitHub
The “driver joystick profile” simply holds several variables that map axis numbers for a specific model of joystick. However, my team likes to drive using left trigger on an Xbox controller as forward, and right trigger as backward.
Here’s the problem. I need to put in the profile an optional “custom axis behavior”. If this is defined, the code will run that for the “get_____Axis” method, instead of “getRawAxis(joystickProfile.fwdBackAxis)” or whatever the axis role is.
How can I put a code snippet in my profile that will override the default behavior if the snippet exists?

When you create a DriverJoystickProfile you would be setting which axis is which there. A trigger is just an axis so that should work.

I would set these in a constructor for that class and have the variables private.

I think Kaleb is barking up the right tree for the specific scenario you mention.

More generically, I think the first question to answer is one about the architecture and “boundary diagram” of this new class you are attempting to implement.

\What sort of alternate behaviors or custom operations do you want to allow?

It’s clear the answer can’t be “none”. However, the answer also shouldn’t be “whatever the user wants” - the user should be implementing things themselves if that’s the case.

In Java, Lambda’s, anonymous classes, and method references are some of the tools that allow you to pass “functionality” to a method or class (as supposed to the more common operation of passing some “value”). However, all these things start to get beyond my real expertise to advise which one is “best” for a particular case.

You may want to also consider constraining the custom-behaviors which are allowed (ex: just inverting an axis, or subtracting two axes, or max of two axes… pick the useful ones). Once this is done, the “custom” behavior could be added to the configuration and implemented inside the class.


Thanks for your help guys. What I ended up doing was putting the default custom behavior in the base profile class to be overridden if necessary. Then I call it there from the joystick wrapper. Simple.
I should clarify the end goal here is to have easily switchable driving setups for a variety of reasons. We won’t be doing anything too crazy with the custom behavior but I wanted to put it in the base code so we have the option.

1 Like