Static Factory Method creates a new object each time it is called

In Effective Java, it mentions that "Unlike constructors, static factory methods are not required to create a new object each time they are called."

class Car{
     String color;
     Boolean spoiler;

     public Car(String s){
        color=s;
        spoiler = false;
     }

     public static Car redCar(){
        return new Car("red");
    }
  }

In the main class:

    Car c2 = Car.redCar();
    Car c3 = Car.redCar();        

c2 and c3 are different objects. I did not get the context "it is not required to create a new object with every call."

+4
source share
8 answers

Because you are doing this:

public static Car redCar(){
    return new Car("red");
}
        //  ^ here

If you want to return the same value, you can do something like:

private static final Car RED_CAR = new Car("red");

public static Car redCar(){
    return RED_CAR;
}

The fact is that the call new Car()will always return a new instance. A call Car.newInstance()means that the class Carcan decide what to do.

For instance:

private static final Map<String, Car> CARS = new HashMap<>();

public static Car newInstance(final String colour){
    return CARS.computeIfAbsent(colour, Car::new);
}

Car Map.computeIfAbsent, , Car Map. () .

:

final Car one = Car.newInstance("red");
final Car two = Car.newInstance("red");
System.out.println(one == two) // true
+6

, - , , factory . ( ):

class Car {
    String color;

    public Car(String color) {
        this.color = color;
    }

    public static Car car(String color) {
        Car car = CARS.get(color);
        if (car != null) return car;
        car = new Car(color);
        CARS.put(color, car);
        return car;
    }

    private static final Map<String, Car> CARS = new HashMap<>();
}

Integer factory valueOf. , factory ( ).

+2

" factory , ". , factory ( ), , ( ). p >

, , redCar() -, :

class Car{
     /* snipped */

     private static final RED = new Car("red");

     public static Car redCar(){
        return RED;
     }
}
+2

,

return new Car("red");  

factory factory.

+1

, , . "" , ; .

unlike constructors static factory methods are not required to create a new object each time they're invoked", , new; , , "" .

: ""; ; Java . , .

, .

+1

Factory - . , , factory.

, singleton . , File, file1.txt, singleton ( ). , file2.text, singleton. File, file1.text file2.text, .

, (, ). , Singleton ( ) , .

, - factory , , ( - ), , , , .

, factory. ( ). factory, ClassName.factory .

+1

The idea of ​​Bloch describes that a static factory can use the pool or cache of instances that it passes on request or makes decisions about its internal logic to create a new instance (which can cache too). Usually this only works for immutable objects , because otherwise you will have some kind of opaque cross-objects.

0
source

The implementation you specified is not a static factory. You have created a class as shown below:

class Car{
     String color;
     Boolean spoiler;
     public static final Car car = new Car("name");

     public Car getInstance(){
        return car;
     }
     private Car(String s){
        color=s;
        spoiler = false;
     }

     public static Car redCar(){
        return new Car("red");
    }
  }

and then in main you have to call

Car.getInstance();
0
source

Source: https://habr.com/ru/post/1584014/


All Articles