Search code examples
javaclassdesign-patternsmethodscode-organization

Coding style and organization


So I am currently making a airplane reservation system for a summer project to keep fresh with Java. With any reservation system its requiring a lot of classes and methods. Currently I'm working on importing the fleet.

My main method is acting like the chronological guide to my program.

public static void main(String[] args){
    //start here 
    //accept passenger credentials 
    //place passenger in seat on plane
}

My question is a formatting problem. When I'm looking to start "making" my aircraft for my fleet. It goes a little like this.

//...
Airplane Boeing737 = new Airplane(seats[], nameOfAircraft);

This will put all values that i need to construct my airplane, obviously there are more variables for the airplane constructor.

My thought is to make a method in the Airplane class that will do this for me. but in order to do this i need to call a blank constructor for the other class (the one with my main method) to see it. I feel like this is horrible form for some reason. Is there a better way to do this?

Another thought as I'm posting is to modify the constructor to not accept any arguments and have that do everything in there. I feel like that's what I should be doing but I'm not 100% sure that would be the correct choice. I guess my overall question would be what are best practices in situations like this.


Solution

  • Use builder pattern, this will allow you:

    • dynamic way of building events
    • maintainable code (you can add more params when you want)
    • preserve integrity of the objects when created

    Joshua Bloch's in Effective Java Chapter 1 Item 2 states:

    Luckily, there is a third alternative that combines the safety of the telescoping constructor pattern with the readability of the JavaBeans pattern. It is a form of the Builder pattern. Instead of making the desired object directly, the client calls a constructor (or static factory) with all of the required parameters and gets a builder object.

    Modifying his example:

    //Builder Pattern
    public class Airplane  {
        private final int[] seats;
        private final String name;
        private final int maxSpeed;
        private final int maxPassengers;
    
        public static class Builder {
            // Required parameters
            private final int[] seats;
            private final String name;
    
            // Optional parameters - initialized to default values
            private int maxSpeed = 1000;
            private int maxPassengers = 150;
    
            public Builder(int[] seats, String name) {
                this.seats = seats;
                this.name = name;
            }
    
            public Builder maxSpeed(int val) {
                maxSpeed = val;
                return this;
            }
    
            public Builder maxPassengers(int val) {
                maxPassengers = val;
                return this;
            }
    
            public Airplane build() {
                return new Airplane(this);
            }
        }
    
        private Airplane(Builder builder) {
            seats = builder.seats;
            name = builder.name;
            maxSpeed = builder.maxSpeed;
            maxPassengers = builder.maxPassengers;
        }
    }
    

    Then you can create several different airplanes

    public static void main(String[] args) {
        // only mandatory params
        Airplane boeing747  = new Airplane.Builder(new int[] {1,0,1}, "boeing747").build();
        // just one param
        Airplane boeing646  = new Airplane.Builder(new int[] {1,1,1}, "boeing646").maxPassengers(250).build();
        // all params
        Airplane fighter    = new Airplane.Builder(new int[] {1,0,0}, "fighter_1").maxPassengers(3).maxSpeed(1600).build();
    }