Created
August 5, 2019 08:42
-
-
Save ethanhuang13/17a235284fd6610d5a1a37ecf75e0bc1 to your computer and use it in GitHub Desktop.
Tracker API Poll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let tracker = Tracker() | |
// Which one do you prefer? Consider labels, indendentation, readability... | |
// 1 func trackEvent(_ event: Event, _ parameters: EventParameter...) | |
tracker.trackEvent(.viewItem, | |
.itemId("12345"), | |
.itemName("An Item"), | |
.list("List name")) | |
// 2. func track(event: Event, parameters: EventParameter...) | |
tracker.track(event: .viewItem, parameters: | |
.itemId("12345"), // Xcode weird indent 這一行排版會歪掉 | |
.itemName("An Item"), | |
.list("List name")) | |
// 3. func track(_ event: Event, parameters: [EventParameter]) { } | |
tracker.track(.viewItem, parameters: [ | |
.itemId("12345"), | |
.itemName("An Item"), | |
.list("List name") | |
]) | |
// 4. func track(event: Event, _ parameters: EventParameter...) { } | |
tracker.track(event: .viewItem, | |
.itemId("12345"), | |
.itemName("An Item"), | |
.list("List name")) |
tracker.event(.viewItem)
.itemId("12345")
.itemName("An Item")
.list("List name")
.track()
還是你要不要試試看 Java 風格? XD
let builder = EventBuilder(. viewItem)
builder.itemId ="12345"
builder.itemName = "An Item"
builder.list = "List name"
tracker.trace(builder.build())
Builder Pattern 加上 Factory Pattern 更有味道。
let factory = EventFactory()
let builder = factory.event(. viewItem)
builder.itemId ="12345"
builder.itemName = "An Item"
builder.list = "List name"
tracker.trace(builder.build())
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Function Builder 版本: