Mike Howard
Mike Howard

Reputation: 53

object reference passed through constructor

Im building a relatively large object-oriented program. I have a class called AerodynamicCalculator that performs numerous calculations and distributes the results around the system. My main concern is that my constructor signature is getting larger and larger as I add mor parameters to it.

As shown below I already have nine object references being passed into this constructor, but I need a further seven. Am I correctly creating this object? My understanding is that you pass the associated object references to the constructor and assign the class'es local variable to the object references. If this is the case the only way to get my class properly initialized with all the required objects is to pass them to the constructor, which is leading to a very long signature.

public AreodynamicCalculator(AircraftConfiguration config, AileronOne aOne,
        AileronTwo aTwo, ElevatorOne eOne, ElevatorTwo eTwo, Rudder r,
        Rudder rr, RateGyros rG) {
    // ...
}

Any advice on this approach would be very helpful, thanks in advance.

Upvotes: 0

Views: 2376

Answers (2)

Mike Pone
Mike Pone

Reputation: 19320

Similarly to using the builder pattern, suggested in daveb's response, you can use a Dependency Injection framework like Spring.

Upvotes: 0

daveb
daveb

Reputation: 76171

As mentioned - this may be a sign your class is doing too much, however, there is a commonly used 'solution' to this problem.

The builder pattern is often used in this situation, but it's also very useful when you have many constructors with different arguments, the builder is good because it makes the meaning of the arguments clearer, particularly when boolean literals are used.

Here is the builder pattern, the way this works is like this:

AreodynamicCalculator calc = AreodynamicCalculator.builder()
    .config(theAircraftConfiguration)
    .addAileron(aileronOne)
    .addAileron(aileronTwo)
    .addElevator(elevatorOne)
    .addElevator(elevatorTwo)
    .addRudder(rudderOne)
    .addRudder(rudderTwo)
    .build()

Internally, the builder will store all these fields, and when build() is called it will call a (now private) constructor that takes these fields:

class AreodynamicCalculator {
    public static class Builder {
        AircraftConfiguration config;
        Aileron aileronOne;
        Aileron aileronTwo;
        Elevator elevatorOne;
        Elevator elevatorTwo;
        ...

        public Builder config(AircraftConfiguration config) {
            this.config = config;
            return this;
        }

        public Builder addAileron(Aileron aileron) {
            if (this.aileronOne == null) {
                this.aileronOne = aileron;
            } else {
                this.aileronTwo = aileron;
            }
            return this;
        }

        // adders / setters for other fields.

        public AreodynamicCalculator build() {
            return new AreodynamicCalculator(config, aileronOne, aileronTwo ... );
        }
    }

    // this is the AircraftConfiguration constructor, it's now private because
    // the way to create AircraftConfiguration objects is via the builder
    //
    private AircraftConfiguration config, AileronOne aOne, AileronTwo aTwo, ElevatorOne eOne, ElevatorTwo eTwo, Rudder r, Rudder rr, RateGyros rG) {
        /// assign fields
    }
}

Upvotes: 6

Related Questions