簡體   English   中英

如何在方法上使用 Lombok @Builder 注釋

[英]How to use Lombok @Builder annotation on Methods

我想有一種簡單的方法來構建測試數據,並且發現 Builder 模式非常適合此處所述。 然而,為了減少組件測試中的樣板代碼,我發現 Project Lombok 中的@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 on 方法創建DogCat實例。

在這個例子中, @Value使用訪問器方法(getter)、一個全參數構造函數、 equals()hashCode()toString()創建了一個最終的不​​可變值對象。

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());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM