728x90
반응형
자바
스트링빌더와 버퍼
StringBuilder
- 스트링 빌더 , 동기화 안쓰는 api
StringBuilder stringBuilder = new StringBuilder();
String result = stringBuilder.append("whiteship").append("keesum").toString();
System.out.println(result);
- 일종의 빌더패턴이라고 할 수 있음
Stream.Builder
Stream.Builder<String> stringStreamBuilder = Stream.builder();
stream<String> names = stringStreamBuilder.add("keesun").add("whiteship").build();
names.forEach(System.out::println);
- 스트링 만들때 빌더와 비슷하게 만들 수 있음
stream<String> names = Stream.<String>builder().add("keesun").add("whiteship").build();
names.forEach(System.out::println);
- 재네릭을 이용해서 줄일 수 있음
lombok 애노테이션
- 디펜던시 추가하고
- Enable annotation 그거 활성화해주면됨
package me.whiteship.designpatterns._01_creational_patterns._04_builder._03_java;
import lombok.Builder;
@Builder
public class LombokExample {
private String title;
private int nights;
private int days;
public static void main(String[] args) {
LombokExample trip = LombokExample.builder()
.title("여행")
.nights(2)
.days(3)
.build();
}
}
- 알아서 빌더패턴같이 사용할 수 있음
스프링
package me.whiteship.designpatterns._01_creational_patterns._04_builder._03_java;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
public class SpringExample {
public static void main(String[] args) {
UriComponents howToStudyJava = UriComponentsBuilder.newInstance()
.scheme("http")
.host("www.whiteship.me")
.path("java playlist ep1")
.build().encode();
System.out.println(howToStudyJava);
}
}
- 이렇게 하면 uri 안전하게 만들 수 있음
- 이전에 우리가 중복된 변수를 썼는데
- before
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class DefaultTourBuilder implements TourPlanBuilder {
private String title;
private int nights;
private int days;
private LocalDate startDate;
private String whereToStay;
private List<DetailPlan> plans;
@Override
public TourPlanBuilder nightsAndDays(int nights, int days) {
this.nights = nights;
this.days = days;
return this;
}
...
}
// 빌더
package me.whiteship.designpatterns._01_creational_patterns._04_builder._02_after;
import me.whiteship.designpatterns._01_creational_patterns._04_builder._01_before.TourPlan;
import java.time.LocalDate;
public interface TourPlanBuilder {
TourPlanBuilder nightsAndDays(int nights, int days);
TourPlanBuilder title(String title);
TourPlanBuilder startDate(LocalDate localDate);
TourPlanBuilder whereToStay(String whereToStay);
TourPlanBuilder addPlan(int day, String plan);
TourPlan getPlan();
}
- after
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class DefaultTourBuilder implements TourPlanBuilder {
private TourPlan tourPlan;
public TourPlanBuilder newInstance(){
this.tourPlan = new TourPlan();
return this;
}
@Override
public TourPlanBuilder nightsAndDays(int nights, int days) {
this.tourPlan.setNights(nights);// 이렇게 변수를 중복을 가지고 있지 않아도 쓸 수 있음
this.days = days;
return this;
}
...
}
// 빌더
package me.whiteship.designpatterns._01_creational_patterns._04_builder._02_after;
import me.whiteship.designpatterns._01_creational_patterns._04_builder._01_before.TourPlan;
import java.time.LocalDate;
public interface TourPlanBuilder {
TourPlanBuilder newInstance();//이거 선언
TourPlanBuilder nightsAndDays(int nights, int days);
TourPlanBuilder title(String title);
TourPlanBuilder startDate(LocalDate localDate);
TourPlanBuilder whereToStay(String whereToStay);
TourPlanBuilder addPlan(int day, String plan);
TourPlan getPlan();
}
728x90
반응형
'CS Study > 디자인패턴' 카테고리의 다른 글
2021.12.04_프로토타입패턴02.패턴적용하기 (0) | 2021.12.04 |
---|---|
2021.12.02_프로토타입패턴01.패턴소개 (0) | 2021.12.04 |
2021.11.29_빌더패턴03.장점과단점 (0) | 2021.11.29 |
2021.11.27_빌더패턴02.패턴적용하기 (0) | 2021.11.27 |
2021.11.27_빌더패턴01.패턴소개 (0) | 2021.11.27 |
댓글