Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swiftで fluentなインターフェースを実装する

A3e610d8bcbd1089a39b1a8caf972ab6?s=47 sozorogami
February 18, 2015

Swiftで fluentなインターフェースを実装する

fluentなAPIを持った色ジェネレーターをSwiftで実装した話。

参考文献
http://martinfowler.com/bliki/FluentInterface.html
http://en.wikipedia.org/wiki/Fluent_interface

A3e610d8bcbd1089a39b1a8caf972ab6?s=128

sozorogami

February 18, 2015
Tweet

Transcript

  1. 4XJGUͰ qVFOUͳΠϯλʔϑΣʔε Λ࣮૷͢Δ Tyler Tape github.com/sozorogami/ @sozorogami_jp

  2. 'MVFOU*OUFSGBDFͱ͸ʁ .BSUJO'PXMFS 

  3. ϝιουݺͼग़͠Λ࿈࠯ͯ͠ ಡΈ΍͘͢

  4. private void makeNormal(Customer customer) { Order o1 = new Order();

    customer.addOrder(o1); OrderLine line1 = new OrderLine(6, Product.find("TAL")); o1.addLine(line1); OrderLine line2 = new OrderLine(5, Product.find("HPK")); o1.addLine(line2); OrderLine line3 = new OrderLine(3, Product.find("LGV")); o1.addLine(line3); line2.setSkippable(true); o1.setRush(true); } ຊདྷ
  5. private void makeFluent(Customer customer) { customer.newOrder() .with(6, "TAL") .with(5, "HPK").skippable()

    .with(3, "LGV") .priorityRush(); } 'MVFOUͳελΠϧ
  6. Customer *customer = [Customer new]; [[[[[[customer newOrder] withAmount:@6 ofItem:@"TAL"] withAmount:@5

    ofItem:@"HPK"] skippable] withAmount:@3 ofItem:@"LGV"] priorityRush]; 0CK$Ͱ͸ɾɾɾ ƅЧƅʀ)
  7. ͦ͜Ͱɾɾɾ HJUIVCDPNTP[PSPHBNJ 4BDL0G3BJOCPXT

  8. let singleColor = theColor(red) singleColor.next()! singleColor.next()

  9. let manyTimes = theColor(green).times(3) manyTimes.next()! manyTimes.next()! manyTimes.next()! manyTimes.next()

  10. let infinity = theColor(yellow).forever() infinity.next()! infinity.next()! infinity.next()! infinity.next()!

  11. let chain = theColor(orange).then(theColor(purple)).forever() chain.next()! chain.next()! chain.next()! chain.next()!

  12. let yellowToBrown = gradient().from(yellow).to(brown).times(10) yellowToBrown.next()! ...

  13. let whiteToBlue = gradient().from(white).to(blue) .times(4) let blueToWhite = gradient().from(blue).to(white) .times(4)

    let waves = whiteToBlue.then(blueToWhite) .forever() waves.next()! ...
  14. ࣮૷ TFMGΛ ฦ͢

  15. ଞͷྫ 2VJDL .BTPOSZ github.com/Quick/Quick github.com/Masonry/Masonry make.top.equalTo(superview.mas_top).with.offset(padding.top); expect(click.hasHighFrequency).to(beTruthy())

  16. ࢀߟจݙ http://martinfowler.com/bliki/FluentInterface.html http://en.wikipedia.org/wiki/Fluent_interface

  17. δϣΠφε