Reputation: 8580
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?
Upvotes: 3
Views: 119
Reputation: 405735
If I understand you correctly, you can create an abstract class AbstractView
that extends View
and implements your common init()
method. AbstractView
should declare the abstract methods a()
and b()
, but leave the implementations to the subclasses.
public abstract class AbstractView extends View {
public void init() {
// common init implementation here
}
public abstract void a();
public abstract void b();
}
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 extend AbstractView
and implement their own a()
and b()
methods, but inherit the common init()
method (along with all of the methods implemented in the View
class. You can then create delegate methods for the exising behavior in ImageView
, ViewGroup
, TextView
, etc. that you need to keep unchanged.
public class MyImageView extends AbstractView {
private ImageView wrappedImageView;
public MyImageView(Context context) {
wrappedImageView = new ImageView(context);
}
// TODO: Implement other constructor wrappers
final void clearColorFilter() {
wrappedImageView.clearColorFilter();
}
// TODO: Implement other method wrappers
@Override
public void a() {
// specific implementation here
}
@Override
public void b() {
// specific implementation here
}
}
Upvotes: 1
Reputation: 612
You can define a class hierarchy which implement methods init() a() b(). Add content in steps with depending on the child class in the hierarchy. In the end your view will call methods on this class and generate content for itself.
interface WidgetContent {
void init();
void a();
void b();
}
abstract class AbstractWidgetContent implements WidgetContent {
public void init(){}
public abstract void a();
public abstract void b();
}
class BaseViewWidgetContent extends AbstractWidgetContent {
public void a() {
}
public void b() {
}
}
class ImageViewWidgetContent extends BaseViewWidgetContent {
public void a() {
}
public void b() {
}
}
class PictureView extends ImageView{
ImageViewWidgetContent imageContent;
}
Upvotes: 0
Reputation: 12048
If I correctly undestood your answer, what you should do is the following:
public abstract class MyBaseView extends View{
public MyBaseView(Context context) {
super(context);
init();
// TODO Auto-generated constructor stub
}
private void init(){
}
public abstract void a();
public abstract void b();
}
and then extend it again using:
public class MyView extends MyBaseView{
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public void a() {
// TODO Auto-generated method stub
}
@Override
public void b() {
// TODO Auto-generated method stub
}
}
Upvotes: 0
Reputation: 5045
You need a new abstract class with the init() method:
public abstract class MyAbstractView extends View {
public void init() {
//TODO: implement init
}
public abstract void a();
public abstract void b();
}
Let your classes (ImageView, TextView) inherit from this abstract class.
Upvotes: 0