Skip to content

Instantly share code, notes, and snippets.

@le0nidas
Created March 8, 2020 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save le0nidas/194796e662390849c9ea95efa8d9369c to your computer and use it in GitHub Desktop.
Save le0nidas/194796e662390849c9ea95efa8d9369c to your computer and use it in GitHub Desktop.
class PhoneNumberScreen private constructor(
private val phoneNumber: PhoneNumber
) {
fun render() {
println("Phone number: ${phoneNumber.value}")
}
companion object {
fun create(phoneNumber: PhoneNumber): PhoneNumberScreen? {
return when {
phoneNumber.value.isNotEmpty() -> PhoneNumberScreen(phoneNumber)
else -> null
}
}
}
}
fun main() {
val contact = Contact(PhoneNumber("12345"))
show(contact.phoneNumber) // "Phone number: 12345"
val contactWithInvalidPhoneNumber = Contact(PhoneNumber(""))
show(contactWithInvalidPhoneNumber.phoneNumber) // prints nothing
}
private fun show(phoneNumber: PhoneNumber) {
val phoneNumberScreen = PhoneNumberScreen.create(phoneNumber)
phoneNumberScreen?.render()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment