복잡하게 생성되어야 할 객체의 생성과 프로퍼티의 표현을 분리하는 패턴
어떤 객체를 생성자로 만들 때 요소가 많다면 여러 단계로 나누어 객체를 만들 수 있게 하는 패턴입니당!
특정 객체를 생성할 때 지정해줘야 할 프로퍼티가 복잡한 경우에 많이 사용됨
Director가 입력을 받고 Builder가 입력을 사용하여 객체를 만드는 방법을 추상화하여 객체를 생성하게 됩니다.
class Director {
func makeLabel(builder: Builder) -> UILabel {
let build = builder
build.setText(with: "희재")
build.setTextColor(with: .black)
build.setFontSize(with: 40)
return build.label
}
}
protocol Builder {
var label: UILabel { get }
func setText(with text: String) -> Builder
func setTextColor(with textColor: UIColor) -> Builder
func setFontSize(with textFontSize: CGFloat) -> Builder
}
class ConCreateBuilder: Builder {
var label: UILabel = UILabel()
func setText(with text: String) -> Builder {
label.text = text
return self
}
func setTextColor(with textColor: UIColor) -> Builder {
label.textColor = textColor
return self
}
func setFontSize(with textFontSize: CGFloat) -> Builder {
label.font = .systemFont(ofSize: textFontSize)
return self
}
}