1 Ocak 2024 Pazartesi

JEP 359 record ve Builder

Örnek
Elimizde şöyle bir kod olsun
public record FilmWithRecord(String title, String director, int releaseYear) {
  
public static class Builder {
  private String title;
  private String director;
  private int releaseYear;

  public Builder title(String title) {
   this.title = title;
   return this;
  }

  public Builder director(String director) {
   this.director = director;
   return this;
  }

  public Builder releaseYear(int releaseYear) {
   this.releaseYear = releaseYear;
   return this;
  }

  public FilmWithRecord build() {
   return new FilmWithRecord(title, director, releaseYear);
  }
  }
}
Kullanmak için şöyle yaparız
// Example of using the builder:
FilmWithRecord film = new FilmWithRecord
 .Builder()
 .title("The Dark Knight")
 .director("Christopher Nolan")
 .releaseYear(2008)
 .build();

Hiç yorum yok:

Yorum Gönder