빌더 패턴
참가자들
암호
public class Main {
public static void main(String[] args) {
Director director = new Director();
Builder b1 = new ConcreteBuilder1();
Builder b2 = new ConcreteBuilder2();
director.construct(b1);
Product p1 = b1.getResult();
p1.show();
director.construct(b2);
Product p2 = b2.getResult();
p2.show();
}
}
public class Director {
public void construct(Builder builder) {
builder.buildPartA();
builder.buildPartB();
}
}
public interface Builder {
Product getResult();
void buildPartA();
void buildPartB();
}
public class ConcreteBuilder1 implements Builder {
private Product product = new Product();
@Override
public Product getResult() {
return product;
}
@Override
public void buildPartA() {
product.add("PartA");
}
@Override
public void buildPartB() {
product.add("PartB");
}
}
public class ConcreteBuilder2 implements Builder {
private Product product = new Product();
@Override
public Product getResult() {
return product;
}
@Override
public void buildPartA() {
product.add("PartX");
}
@Override
public void buildPartB() {
product.add("PartY");
}
}
public class Product {
private List<String> parts = new ArrayList<>();
public void show() {
System.out.println("\nProduct Parts -------");
for (String part : parts) {
System.out.println(part);
}
}
public void add(String part) {
parts.add(part);
}
}
산출
Product Parts -------
PartA
PartB
Product Parts -------
PartX
PartY
eidherjulian61
/
디자인 패턴
주요 디자인 패턴
eidher ・ 2020년 9월 27일 ・ 1분 읽기
#designpatterns
#creational
#structural
#behavioral
Reference
이 문제에 관하여(빌더 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://dev.to/eidher/builder-patter-12jc
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
public class Main {
public static void main(String[] args) {
Director director = new Director();
Builder b1 = new ConcreteBuilder1();
Builder b2 = new ConcreteBuilder2();
director.construct(b1);
Product p1 = b1.getResult();
p1.show();
director.construct(b2);
Product p2 = b2.getResult();
p2.show();
}
}
public class Director {
public void construct(Builder builder) {
builder.buildPartA();
builder.buildPartB();
}
}
public interface Builder {
Product getResult();
void buildPartA();
void buildPartB();
}
public class ConcreteBuilder1 implements Builder {
private Product product = new Product();
@Override
public Product getResult() {
return product;
}
@Override
public void buildPartA() {
product.add("PartA");
}
@Override
public void buildPartB() {
product.add("PartB");
}
}
public class ConcreteBuilder2 implements Builder {
private Product product = new Product();
@Override
public Product getResult() {
return product;
}
@Override
public void buildPartA() {
product.add("PartX");
}
@Override
public void buildPartB() {
product.add("PartY");
}
}
public class Product {
private List<String> parts = new ArrayList<>();
public void show() {
System.out.println("\nProduct Parts -------");
for (String part : parts) {
System.out.println(part);
}
}
public void add(String part) {
parts.add(part);
}
}
Product Parts -------
PartA
PartB
Product Parts -------
PartX
PartY
eidherjulian61 / 디자인 패턴
주요 디자인 패턴
eidher ・ 2020년 9월 27일 ・ 1분 읽기
#designpatterns
#creational
#structural
#behavioral
Reference
이 문제에 관하여(빌더 패턴), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/eidher/builder-patter-12jc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)