- Kotlin Standard Library Cookbook
- Samuel Urbanowicz
- 239字
- 2021-07-23 19:05:54
How it works...
Now, let's give it a try and take a look at how we can use the EmailValidator interface in action. Let's assume we have a RegistrationForm class containing a hook method that is invoked every time the input text is modified:
class RegistrationForm() {
fun onInputTextUpdated(text: String) {
// do some actions on text changed
}
}
To make use of our EmailValidator interface, we need to declare a class that is implementing it. We can modify the RegistrationForm class to implement the EmailValidator interface:
class RegistrationForm(override var input: String = ""): EmailValidator {
fun onInputTextUpdated(newText: String) {
this.input = newText
if (!isEmailValid()) {
print("Wait! You've entered wrong email...")
} else {
print("Email is correct, thanks: ${getUserLogin()}!")
}
}
}
Every time the onInputUpdated() function is invoked, we are updating the input: String property declared in the EmailValidator interface. Once it is up to date, we are using the EmailValidator interface functions isEmailValid() and getUserLogin() values. Extracting the function implementations to the interface makes it possible to reuse them and integrate them easily in a number of classes. The only part that needs an actual implementation is the input property of the EmailValidator interface, which holds the current state of the text inserted by the user. The smooth way of integrating the EmailValidator interface makes it great when it comes to reusability and versatility of the application in different scenarios.
- 精通Nginx(第2版)
- Learn Type:Driven Development
- Raspberry Pi Networking Cookbook(Second Edition)
- iOS開發實戰:從零基礎到App Store上架
- 微信公眾平臺開發:從零基礎到ThinkPHP5高性能框架實踐
- 用戶體驗增長:數字化·智能化·綠色化
- C++對象模型詳解
- 輕松上手2D游戲開發:Unity入門
- Kotlin開發教程(全2冊)
- Geospatial Development By Example with Python
- Red Hat Enterprise Linux Troubleshooting Guide
- Java并發編程之美
- Python滲透測試編程技術:方法與實踐(第2版)
- Learning NHibernate 4
- ASP.NET 3.5系統開發精髓