官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 津南区| 龙岩市| 阳信县| 德钦县| 苍梧县| 新沂市| 临夏市| 铜川市| 慈利县| 东方市| 卫辉市| 革吉县| 安义县| 唐山市| 沙洋县| 阳西县| 房山区| 都江堰市| 额尔古纳市| 太仆寺旗| 鄂州市| 清镇市| 肇东市| 化德县| 自治县| 新宁县| 临沭县| 墨竹工卡县| 赤峰市| 桑植县| 偏关县| 鄂州市| 清徐县| 安徽省| 巫山县| 宁城县| 平凉市| 崇阳县| 沽源县| 华蓥市| 新乡县|