Static Factory
With static factory method you don't have to create an object every time.
/**
* Static factory methods return an instance of a class.
*
* One advantage is that, unlike constructors, they have names.
* The resulting code is easier to read.
*
* Second, you don't need to create an object every time.
*/
package com.minte9.effective.constructors;
public class StaticFactory {
public static void main(String[] args) {
MyClass.getInstance();
// Class constructor
MyClass.getInstance();
// nothing
}
}
class MyClass {
private static final MyClass INSTANCE = new MyClass();
public static MyClass getInstance() { // Look Here
return INSTANCE;
}
private MyClass() {
System.out.println("Class constructor");
}
}
Telescoping
Programmers use telescoping constructor when faced with many parameters.
/**
* Telescoping constructors works, but ...
* it is hard to write and hard to read.
*/
package com.minte9.effective.constructors;
public class Telescoping {
public static void main(String[] args) {
new Nutrition(240);
new Nutrition(240, 100);
new Nutrition(240, 100, 10);
/*
Constructor (size, calories, fat) 240 0 0
Constructor (size, calories, fat) 240 100 0
Constructor (size, calories, fat) 240 100 10
*/
}
}
class Nutrition {
public Nutrition(int size) { // one param always required
this(size, 0);
}
public Nutrition(int size, int calories) {
this(size, calories, 0);
}
public Nutrition(int size, int calories, int fat) {
System.out.println(
"Constructor (size, calories, fat) "
+ size + " "
+ calories + " "
+ fat
);
}
}
JavaBeans
A second alternative is to use setter methods (JavaBeans pattern).
/**
* A JavaBeans constructor object ...
* may be in an inconsistent state
*/
package com.minte9.effective.constructors;
public class Javabeans {
public static void main(String[] args) {
A a = new A();
a.setSize(240);
// setSize: 240
a.setCalories(100);
// setCalories: 100
a.setFat(11);
// setFat: 11
}
}
class A {
private int size = -1; // required, no default
private int calories = 0;
private int fat = 0;
public void setSize(int s) {
size = s;
System.out.println("setSize: " + size);
}
public void setCalories(int s) {
calories = s;
System.out.println("setCalories: " + calories);
}
public void setFat(int s) {
fat = s;
System.out.println("setFat: " + fat);
}
}
Builder
A Builder combines the safety and readability of both Telescoping and JavaBeans.
/**
* Consider a builder when faced with many constructor parameters
*/
package com.minte9.effective.constructors;
public class BuilderApp {
public static void main(String[] args) {
N n = new N.Builder(240)
.calories(100)
.fat(11)
.build();
System.out.println(n.getFat());
// 11
}
}
class N {
private final int size;
private final int calories;
private final int fat;
public static class Builder {
private int size;
private int calories = 0;
private int fat = 0;
public Builder(int s) {
size = s;
}
public Builder calories(int c) {
calories = c;
return this;
}
public Builder fat(int f) {
fat = f;
return this;
}
public N build() {
return new N(this);
}
}
private N(Builder builder) {
size = builder.size;
calories = builder.calories;
fat = builder.fat;
}
public int getSize() {
return size;
}
public int getCalories() {
return calories;
}
public int getFat() {
return fat;
}
}
Last update: 357 days ago