-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathfactory2.java
33 lines (26 loc) · 841 Bytes
/
factory2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package factory;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;
public interface factory2 {
enum Color { RED, BLUE }
interface Vehicle { }
record Car(Color color) implements Vehicle { }
record Bus(Color color) implements Vehicle { }
@FunctionalInterface
interface VehicleFactory {
Vehicle create(Color color);
default Supplier<Vehicle> bind(Color color) {
return () -> create(color);
}
}
static List<Vehicle> create5(Supplier<Vehicle> factory) {
return Stream.generate(factory).limit(5).toList();
}
static void main(String[] args) {
VehicleFactory carFactory = Car::new;
VehicleFactory busFactory = Bus::new;
System.out.println(create5(carFactory.bind(Color.RED)));
System.out.println(create5(busFactory.bind(Color.BLUE)));
}
}