Question

I am currently making my method call in the following way:

InstrumentsInfo instrumentsInfo = new InstrumentsInfo();
String shortInstruName = "EURUSD"

TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo.getInstrumentID(shortInstruName), instrumentsInfo.getInstrumentTickSize(shortInstruName), instrumentsInfo.getInstrumentName(shortInstruName));

In VBA I would do something like this

With instrumentsInfo
 TrackInstruments(.getInstrumentID(shortInstruName), .getInstrumentTickSize(shortInstruName), .getInstrumentName(shortInstruName));

So my question is, is there a way to avoid repeating "instrumentsInfo" in the method call in Java?

Was it helpful?

Solution

In a word no although you may want to consider changing

TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo.getInstrumentID(shortInstruName), instrumentsInfo.getInstrumentTickSize(shortInstruName), instrumentsInfo.getInstrumentName(shortInstruName));

to

TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo);

and then have the constructor take the parameters it needs.

Or perhaps use the builder pattern if you need a lot of parameters.

Or indeed ask yourself why you are constructing InstrumentsInfo outside the TrackInstruments when the latter seems to rely on it so heavily. (Without fully understanding your objects that is)

OTHER TIPS

Yes, you can create a constructor in TrackInstruments that accepts the object type InstrumentsInfo

TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo);

No, there is no With syntax in Java as such. To avoid repeating "instrumentsInfo", you could, however, just create a constructor which takes the type:

TrackInstruments trackInstruments = new TrackInstruments(instrumentsInfo);

This design, however, leads to the TrackInstruments knowing about an InstrumentsInfo which does not promote loose coupling between objects, so you could use:

Integer instrumentID = instrumentsInfo.getInstrumentID(shortInstruName);
Integer instrumentTickSize = instrumentsInfo.getInstrumentTickSize(shortInstruName);
String instrumentName = instrumentsInfo.getInstrumentName(shortInstruName);

TrackInstruments trackInstruments = new TrackInstruments(instrumentID, instrumentTickSize, instrumentName);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top