Skip to content

Instantly share code, notes, and snippets.

@minibugdev
Last active July 20, 2016 08:26
Show Gist options
  • Save minibugdev/068b3df36cf78c390cd4977efdc9f402 to your computer and use it in GitHub Desktop.
Save minibugdev/068b3df36cf78c390cd4977efdc9f402 to your computer and use it in GitHub Desktop.
abstract class Validator() {
var errorListener: OnErrorListener? = null
var validateValue: Any? = null
protected abstract fun hasError(): Boolean
fun validate(): Boolean {
if (hasError()) {
errorListener?.onError()
return false
}
return true
}
interface OnErrorListener {
fun onError()
}
}
class Main {
val validator = ExtentedValidator()
fun testValidator() {
validator.errorListener = object : ValidationRule.OnErrorListener {
override fun onError() {
// show error
}
}
if(validator.validate()) {
// Do something ...
}
}
}
@VerachadW
Copy link

Try to use Lambda instead of listener interface.

var errorListener: (()->Unit)? = null

and change calling statement to

errorListener?.invoke()

Now in Main class, we can change to

validator.errorListener = {
  //show error
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment