Ive got a few instances of subclasses of View (like ImageView, ViewGroup, TextView, etc) , which i have them all implement two methods a() and b() and they all have to run the same init() method which is long and does the same for all of them.
how should i design my architecture to enhance code reusability in that case?
if it was C++ i could multi inherite from view and a new abstract class that runs init() on time of creation and have abstarct methods a() and b(), how is it acheived in Java?
maybe there’s some way to acheive it using some kind of a decorator?
If I understand you correctly, you can create an abstract class
AbstractViewthat extendsViewand implements your commoninit()method.AbstractViewshould declare the abstract methodsa()andb(), but leave the implementations to the subclasses.Since you’re trying to add behavior to a group of existing subclasses (
ImageView,ViewGroup,TextView, etc.), you probably do need to create a wrapper for each subclass (MyImageView,MyViewGroup,MyTextView, etc.). Each of these subclasses would extendAbstractViewand implement their owna()andb()methods, but inherit the commoninit()method (along with all of the methods implemented in theViewclass. You can then create delegate methods for the exising behavior inImageView,ViewGroup,TextView, etc. that you need to keep unchanged.