I have multiple views extending Android framework classes :
class A extends ImageView // ImageView extends android.View
class B extends TextView //TextView extends android.View
class C extends LinearLayout //LinearLayout extends android.ViewGroup which extends android.View
I have an operation which can be applied to android.view
void notifyError(){
getParentView().notifyError() //I have type checks on getParentView()
}
The method above is an over-simplified version of one of such methods. The important part is :
- All views should have a notifyError() method
- The code in notifyError is the exactly same for all views
Ideally, I should have an abstract class which all the views can extend. I could have given a base implementation of notifyError() in the abstract class and all good. But this solution is out of scope as a view cannot extend from multiple classes.
The solution I currently have is to have all my views implement an interface, which forces all views to override notifyError(). I created a static helper class which is then called by notifyError(). All my views are having the code to invoke the helper class in their notifyError() methods. This redundancy does not feel right to me.
Is there a better way to achieve this ? In case anyone is curious over the requirement, here it is : If any of my child views get an error, I want to recursively notify all parents of that error (until we reach the topmost parent in view hierarchy). All parents have the same error handling logic for each child.
EDIT: Based on the comments, let me share a simplified error handling logic for notifyError() method :
void notifyError(Exception e){
Log.e(TAG, e.getMessage());
getParentView().notifyError(e);
}