'builder'에 해당되는 글 1건

  1. 2007/01/08 ExpressionBuilder (6)

2007/01/08 00:15 Developer

ExpressionBuilder

요즘 OOP를 좀 한다는 분들과 EDSL에 관심이 많은 분들은 아래와 같은 표현이 이제는 익숙할 것입니다. JMock API의 대화능력에 반하신 분들이죠?

http://martinfowler.com/bliki/DomainSpecificLanguage.html
http://www.mockobjects.com/files/evolving_an_edsl.ooplsa2006.pdf

customer.newOrder()
  .with(6, "TAL")
  .with(5, "HPK").skippable()
  .with(3, "LGV")
  .priorityRush();

하지만, MF의 글중에서 다음과 같은 글도 읽었을 것입니다.
1. 명령과 질의를 분리해라
2. 인터페이스는 간결화하고, 최소화해라.

http://martinfowler.com/bliki/CommandQuerySeparation.html
http://martinfowler.com/bliki/MinimalInterface.html

그러면, API를 디자인을 할때 헷갈리기 시작합니다. 도데체 나보고 어쩌란 얘기냐?
Fluent Interface를 구현하려면, 당연히 Human Interface화 되어야 하는거 아닌가?

물론, evolving_an_edsl.ooplsa2006에서도 관련 내용이 나와있지만, 이런 상황에서 MF아저씨는 다음과 같은 코드로 해결 방법을 가르켜줍니다.

facade or builder틱한 builder layer가 생기겠지만, 위의 원칙들은 지켜줄수 있습니다.

가만히 생각해보면, 알게 모르게 OOP에 있어서 구현과 인터페이스를 분리시켜주는 역할에 builder pattern이 자주 사용되고 있습니다. API를 만들어서 공개를 할때 신경써야 할게 너무 많네요..^^

public class OrderBuilder {
  private Order subject = new Order();
  private OrderLine currentLine;

  public OrderBuilder with(int quantity, String productCode) {
  currentLine = new OrderLine(quantity, Product.find(productCode));
  subject.addLine(currentLine);
  return this;
  }

  public OrderBuilder skippable() {
  currentLine.setSkippable(true);
  return this;
  }

  public OrderBuilder priorityRush() {
  subject.setRush(true);
  return this;
  }

  public Order getSubject() {
  return subject;
  }
}

북치고 장구치고 다하는 MF의 bliki의 글들이었습니다.

ExpressionBuilder
http://martinfowler.com/bliki/ExpressionBuilder.html

Posted by ologist
이전버튼 1 이전버튼

블로그 이미지
ologist

공지사항

Yesterday191
Today137
Total34,709

달력

 « |  » 2012.02
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

최근에 받은 트랙백

글 보관함