1

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 :

  1. All views should have a notifyError() method
  2. 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);
}
dev
  • 11,071
  • 22
  • 74
  • 122
  • 1
    I don't see the problem with the abstract class. You can have your abstract class extend `android.View` and your views extends your abstract class. Otherwise, I don't know if your version of Android allows to use Java 8, but be aware that interfaces can have default (concrete) methods in Java 8. Finally, when inheritance does not work the next option is usually composition – Dici May 10 '16 at 00:52
  • ImageView, TextView etc are framework provided classes with a lot of logic I cannot write on my own. Extending View directly is a nightmare ! – dev May 10 '16 at 00:53
  • Oh ok I thought it was yours – Dici May 10 '16 at 00:54
  • Where does `getParentView` come from ? I don't see it in the doc of `View` (only `getParent`) – Dici May 10 '16 at 00:58
  • Sorry, for clarity I mentioned getParentView() instead. I assumed not everyone would understand getParent(). – dev May 10 '16 at 01:00
  • Got it. Is there a strong requirement for extending these components, or can you afford to just compose them ? It just sounds easier – Dici May 10 '16 at 01:01
  • Yes, extending them is the only option for now. I have been hoping for a way to compose the error handling logic somehow, but can't wrap my head around. – dev May 10 '16 at 01:02
  • You don't give enough details on the error handling then. Given your requirements as currently in the question, I think there is not much room for improvement – Dici May 10 '16 at 01:05

2 Answers2

0

Since Java do not support multi-inheritance, you have to use an Interface.

Possible duplicate: How to inherit from multiple base classes in Java?

So your implementation seams to be good.

Community
  • 1
  • 1
sonique
  • 4,539
  • 2
  • 30
  • 39
0

The default methods in Java 8 interfaces seem to solve this issue. Although, I cannot use Java 8 unless I move my minimum supported android version to 24; this approach would be the right way on platforms which do support Java 8. hence, marking this answer as accepted.

dev
  • 11,071
  • 22
  • 74
  • 122