You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@factory creates a factory method that can create different implementations:
Usage:
@Factory(HexagonImpl.class, TrianglImple.class)
interface Shape {
...
}
// Elsewhere ...
public class HexagonImpl implements Shape { ... }
public class TriangleImpl implements Shape { ... }
// Usage:
// Lombok creates a hidden 'factory' object in the interface Shape.
// The factory object returns a specific implementation.
Shape s = Shape.factory.create(Shape.Type.HEXAGON, ...params);
This is mostly useful for the so-called "sealed factories", where the
implementations types are known at compile time.
Implementation:
Shape.java:
// @Factory({TriangleImpl.class, HexagonImpl.class})
public interface Shape {
/* Business methods. */
int computeArea();
/* ========= Generated by Lombok ========= */
enum Type {TRIANGLE, HEXAGON}; // Enum generated by stripping out 'Impl' from the specified impl classes in the @Factory annotation.
static final ShapeFactory factory = ShapeFactory.instance;
class ShapeFactory {
private static final ShapeFactory instance = new ShapeFactory();
private ShapeFactory() {}
public Shape create(Shape.Type type, Map<String, ?> properties) {
switch (type) {
case TRIANGLE:
return new TriangleImpl(properties);
case HEXAGON:
return new HexagonImpl(properties);
}
throw new IllegalArgumentException("Unknown shape type: " + type);
}
}
/* ========= End generated ========= */
}
TriangleImpl.java:
class TriangleImpl implements Shape {
TriangleImpl(Map<String,?> properties) {
}
@Override
public int computeArea() {
// TODO
return 0;
}
}
HexagonImpl.java:
class HexagonImpl implements Shape {
HexagonImpl(Map<String,?> properties) {
}
@Override
public int computeArea() {
// TODO
return 0;
}
}
Usage:
import Shape;
public class FactoryExample {
public static void main() {
Shape hex = Shape.factory.create(Shape.Type.HEXAGON, null);
}
}
The text was updated successfully, but these errors were encountered:
@factory creates a factory method that can create different implementations:
Usage:
This is mostly useful for the so-called "sealed factories", where the
implementations types are known at compile time.
Implementation:
Shape.java:
TriangleImpl.java:
HexagonImpl.java:
Usage:
The text was updated successfully, but these errors were encountered: