REMEMBER JAVA

MINTE9
2REMEMBER




Constructors

p5 The traditional way to allow a client to obtain an instance is with a public constructor.
 RunCode
class App {

    public static void main(String[] args) {

        A A = new A();
        B B = new B();
    }
}

class A {
    // Java provides a default constructor ...
    // with no parameters
}

class B {
    public B() { // defined constructor

    }
}
... 14 lines
˄˄˄
p5 ! With static factory method you don't have to create an object every time.
 RunCode
class App {

    public static void main(String[] args) {

        MyClass.getInstance(); // outputs: constructor
        MyClass.getInstance(); // outputs: nothing
    }
}

class MyClass {

    private static final MyClass INSTANCE = new MyClass();
    public static MyClass getInstance() { return INSTANCE; } // Look Here
    private MyClass() {
        System.out.println("constructor");
    }
}
... 13 lines
˄˄˄
Telescoping    (2/3)

Telescoping

p10 ! Programmers use telescoping constructor when faced with many parameters.
 RunCode
/**
 * Telescoping constructors works, but ...
 * it is hard to write and hard to read
 */
class App {

    public static void main(String[] args) {

        Nutrition n = new Nutrition(240, 8, 100, 0);
    }
}

class Nutrition {

    private final int size;
    private final int servings;
    private final int calories;
    private final int fat;

    public Nutrition(int size, int serv) { // required params
        this(size, serv, 0);
    }

    public Nutrition(int size, int serv, int cal) {
        this(size, serv, cal, 0);
    }

    public Nutrition(int size, int serv, int cal, int fat) {
        this.size = size;
        this.servings = serv;
        this.calories = cal;
        this.fat = fat;
    }
}
... 27 lines
˄˄˄
p12 ! A second alternative is to use setter methods (JavaBeans pattern).
 RunCode
/**
 * A JavaBeans constructor object ...
 * may be in an inconsistent state
 */
class App {

    public static void main(String[] args) {

        Nutrition n = new Nutrition();
        n.setSize(240);
        n.setServings(8);
        n.setCalories(100);

        System.out.println(n.getFat()); // 0
        n.setFat(11);
        
    }
}

class Nutrition {

    private int size = -1; // required, no default
    private int servings = -1; // required, no default
    private int calories = 0;
    private int fat = 0;

    public Nutrition() {}
    public void setSize(int s) { this.size = s; }
    public void setServings(int s) { this.servings = s; }
    public void setCalories(int s) { this.calories = s; }
    public void setFat(int s) { this.fat = s; }

    public int getFat() { return fat; }
}
... 26 lines
˄˄˄
Builder    (3/3)

Builder

p14 A Builder combines the safety and readability of both Telescoping and JavaBeans.
 RunCode
class App {

    public static void main(String[] args) {

        Nutrition n = new Nutrition.Builder(240, 8)
                            .calories(100)
                            .fat(11)
                            .build(); 

        System.out.println(n.getFat()); // 11
    }
}

class Nutrition {

    private final int size;
    private final int servings;
    private final int calories;
    private final int fat;

    public static class Builder {

        private final int size; // required
        private final int servings; // required
        private int calories    = 0;
        private int fat         = 0;
        
        public Builder(int size, int servings) {
            this.size       = size; 
            this.servings   = servings;
        }
        public Builder calories(int val) { calories = val; return this; }
        public Builder fat(int val) { fat = val; return this; }

        public Nutrition build() { return new Nutrition(this); }
    }
    
    private Nutrition(Builder builder) {
        size        = builder.size;
        servings    = builder.servings;
        calories    = builder.calories;
        fat         = builder.fat;
    }

    public int getFat() { return fat; }
}
... 35 lines
˄˄˄

Questions    
No. 56   Dependency Injection
CARD
55

        A B C D E F
🔔
1/3