Skip to content

Instantly share code, notes, and snippets.

@ethanhuang13
Created August 5, 2019 08:42
Show Gist options
  • Save ethanhuang13/17a235284fd6610d5a1a37ecf75e0bc1 to your computer and use it in GitHub Desktop.
Save ethanhuang13/17a235284fd6610d5a1a37ecf75e0bc1 to your computer and use it in GitHub Desktop.
Tracker API Poll
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"))
@zonble
Copy link

zonble commented Aug 5, 2019

Function Builder 版本:

tracker.track(event: .viewItem) {
    .itemId("12345")
    .itemName("An Item")
    .list("List name")
}

@focuspirit
Copy link

focuspirit commented Aug 5, 2019

tracker.event(.viewItem)
.itemId("12345")
.itemName("An Item")
.list("List name")
.track()

@zonble
Copy link

zonble commented Aug 5, 2019

還是你要不要試試看 Java 風格? XD

let builder = EventBuilder(. viewItem)
builder.itemId ="12345"
builder.itemName = "An Item"
builder.list = "List name"
tracker.trace(builder.build())

@zonble
Copy link

zonble commented Aug 5, 2019

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