如何在方法上使用 Lombok @Builder 注释
How to use Lombok @Builder annotation on Methods
我想要一种简单的方法来构建测试数据,并且发现 Builder 模式非常适合,正如 Project Lombok 中描述的那样 here. However to reduce boilerplate codes in the component tests, even more, I have found @Builder 是一个不错的尝试候选者。但是,我找不到任何关于如何在方法上使用它的文档或在线示例。我想在某种工厂方法上使用 @Builder
,因为我无法对实现进行任何更改。
有人可以举例说明如何在方法上实际使用 @Builder
吗?
这就是您的使用方式@Builder。
//Employee.Java
import lombok.Builder;
import lombok.ToString;
@Builder
@ToString
public class Employee {
private final String empName;
private final int salary;
}
// Main.java
public class Main {
public static void main(String[] args) {
Employee emp = Employee.builder().empName("Deendaya").salary(100).build();
System.out.println(emp);
}
}
使用 @Builder
方法创建 Dog
和 Cat
实例。
在此示例中,@Value
使用访问器方法(getter)、全参数构造函数、equals()
、hashCode()
和 toString()
创建了一个最终的不可变值对象。 =19=]
import static org.junit.Assert.*;
import lombok.Builder;
import lombok.Value;
import org.junit.Test;
@SuppressWarnings("javadoc")
public class ImmutableAnimals {
@Builder(builderMethodName = "dogBuilder")
public static Dog newDog(String color, String barkSound) {
return new Dog(color, barkSound);
}
@Builder(builderMethodName = "catBuilder")
public static Cat newCat(String color, String meowSound) {
return new Cat(color, meowSound);
}
public static interface Animal {
String getColor();
}
@Value
public static class Cat implements Animal {
String color;
String meowSound;
}
@Value
public static class Dog implements Animal {
String color;
String barkSound;
}
@Test
public void testDog() {
final String expectedBarkSound = "woof";
final String expectedColor = "brown";
final Dog dog = ImmutableAnimals.dogBuilder()
.barkSound(expectedBarkSound)
.color(expectedColor)
.build();
assertEquals(expectedBarkSound, dog.getBarkSound());
assertEquals(expectedColor, dog.getColor());
}
@Test
public void testCat() {
final String expectedMeowSound = "purr";
final String expectedColor = "white";
final Cat cat = ImmutableAnimals.catBuilder()
.meowSound(expectedMeowSound)
.color(expectedColor)
.build();
assertEquals(expectedMeowSound, cat.getMeowSound());
assertEquals(expectedColor, cat.getColor());
}
}
这是另一个具有相同域 类 但使用可变值的示例。但是,一如既往地支持不变性。
import static org.junit.Assert.*;
import lombok.Builder;
import lombok.Data;
import org.junit.Test;
@SuppressWarnings("javadoc")
public class MutableAnimals {
@Builder(builderMethodName = "dogBuilder")
public static Dog newDog(String color, String barkSound) {
final Dog dog = new Dog();
dog.setBarkSound(barkSound);
dog.setColor(color);
return dog;
}
@Builder(builderMethodName = "catBuilder")
public static Cat newCat(String color, String meowSound) {
final Cat cat = new Cat();
cat.setMeowSound(meowSound);
cat.setColor(color);
return cat;
}
public static interface Animal {
String getColor();
}
@Data
public static class Cat implements Animal {
String color;
String meowSound;
}
@Data
public static class Dog implements Animal {
String color;
String barkSound;
}
@Test
public void testDog() {
final String expectedBarkSound = "woof";
final String expectedColor = "brown";
final Dog dog = MutableAnimals.dogBuilder()
.barkSound(expectedBarkSound)
.color(expectedColor)
.build();
assertEquals(expectedBarkSound, dog.getBarkSound());
assertEquals(expectedColor, dog.getColor());
}
@Test
public void testCat() {
final String expectedMeowSound = "purr";
final String expectedColor = "white";
final Cat cat = MutableAnimals.catBuilder()
.meowSound(expectedMeowSound)
.color(expectedColor)
.build();
assertEquals(expectedMeowSound, cat.getMeowSound());
assertEquals(expectedColor, cat.getColor());
}
}
我想要一种简单的方法来构建测试数据,并且发现 Builder 模式非常适合,正如 Project Lombok 中描述的那样 here. However to reduce boilerplate codes in the component tests, even more, I have found @Builder 是一个不错的尝试候选者。但是,我找不到任何关于如何在方法上使用它的文档或在线示例。我想在某种工厂方法上使用 @Builder
,因为我无法对实现进行任何更改。
有人可以举例说明如何在方法上实际使用 @Builder
吗?
这就是您的使用方式@Builder。
//Employee.Java
import lombok.Builder;
import lombok.ToString;
@Builder
@ToString
public class Employee {
private final String empName;
private final int salary;
}
// Main.java
public class Main {
public static void main(String[] args) {
Employee emp = Employee.builder().empName("Deendaya").salary(100).build();
System.out.println(emp);
}
}
使用 @Builder
方法创建 Dog
和 Cat
实例。
在此示例中,@Value
使用访问器方法(getter)、全参数构造函数、equals()
、hashCode()
和 toString()
创建了一个最终的不可变值对象。 =19=]
import static org.junit.Assert.*;
import lombok.Builder;
import lombok.Value;
import org.junit.Test;
@SuppressWarnings("javadoc")
public class ImmutableAnimals {
@Builder(builderMethodName = "dogBuilder")
public static Dog newDog(String color, String barkSound) {
return new Dog(color, barkSound);
}
@Builder(builderMethodName = "catBuilder")
public static Cat newCat(String color, String meowSound) {
return new Cat(color, meowSound);
}
public static interface Animal {
String getColor();
}
@Value
public static class Cat implements Animal {
String color;
String meowSound;
}
@Value
public static class Dog implements Animal {
String color;
String barkSound;
}
@Test
public void testDog() {
final String expectedBarkSound = "woof";
final String expectedColor = "brown";
final Dog dog = ImmutableAnimals.dogBuilder()
.barkSound(expectedBarkSound)
.color(expectedColor)
.build();
assertEquals(expectedBarkSound, dog.getBarkSound());
assertEquals(expectedColor, dog.getColor());
}
@Test
public void testCat() {
final String expectedMeowSound = "purr";
final String expectedColor = "white";
final Cat cat = ImmutableAnimals.catBuilder()
.meowSound(expectedMeowSound)
.color(expectedColor)
.build();
assertEquals(expectedMeowSound, cat.getMeowSound());
assertEquals(expectedColor, cat.getColor());
}
}
这是另一个具有相同域 类 但使用可变值的示例。但是,一如既往地支持不变性。
import static org.junit.Assert.*;
import lombok.Builder;
import lombok.Data;
import org.junit.Test;
@SuppressWarnings("javadoc")
public class MutableAnimals {
@Builder(builderMethodName = "dogBuilder")
public static Dog newDog(String color, String barkSound) {
final Dog dog = new Dog();
dog.setBarkSound(barkSound);
dog.setColor(color);
return dog;
}
@Builder(builderMethodName = "catBuilder")
public static Cat newCat(String color, String meowSound) {
final Cat cat = new Cat();
cat.setMeowSound(meowSound);
cat.setColor(color);
return cat;
}
public static interface Animal {
String getColor();
}
@Data
public static class Cat implements Animal {
String color;
String meowSound;
}
@Data
public static class Dog implements Animal {
String color;
String barkSound;
}
@Test
public void testDog() {
final String expectedBarkSound = "woof";
final String expectedColor = "brown";
final Dog dog = MutableAnimals.dogBuilder()
.barkSound(expectedBarkSound)
.color(expectedColor)
.build();
assertEquals(expectedBarkSound, dog.getBarkSound());
assertEquals(expectedColor, dog.getColor());
}
@Test
public void testCat() {
final String expectedMeowSound = "purr";
final String expectedColor = "white";
final Cat cat = MutableAnimals.catBuilder()
.meowSound(expectedMeowSound)
.color(expectedColor)
.build();
assertEquals(expectedMeowSound, cat.getMeowSound());
assertEquals(expectedColor, cat.getColor());
}
}