I'm creating an Android application. I need to override the draw method on a number of UI classes to create a custom appearance. These classes all subclass View. I'm wondering what the best way to do this is. I'd like to be able to reuse code as much as possible, so I'm looking for help in organizing things. As I see it right now, I have 2 options:
Option 1 - Subclass Everything
If I want to use LinearLayout, I create CustomLinearLayout. If I want to use ImageView, I create CustomImageView. On each of these custom classes, I override draw exactly the same way. This doesn't seem efficient because I'm repeating code and extending a number of classes which do almost nothing.
Option 2 - Subclass a Super Class
My original thought was to extend View and create CustomView, because it's already a superclass of all the classes I want to use. This, however, doesn't work because all the existing subclasses I want to use are still extending View, not CustomView.
Is there a better way to do this? Am I missing something?
One possible solution would be to extract your draw logic into a separate class DrawingCode. This could contain a static method or you could even use instances of DrawingCode to customize your drawing code with other parameters. Of course you'll still have to overwrite the draw() method, but only write one line of code to call DrawingCode.draw(param1, param2). This way you get to store your drawing code in one central place and don't repeat yourself.
Related
I have a bunch classes that extends another class, and want to change a function inside of it. Normally I'd you'd use an Override here, but since there are so many, I was wondering if there is a way to alter that method for every file in my project. I would simply copy the extended classes code and put it into a file I can alter myself, but I'm afraid I'd break something with how many classes it interacts with.
I was recently making a small program that involves wrapping around a JFreeChart library and made a number of convenience classes to easily make the charts and provide ready objects that exted JPanel and can readily be placed around and realised that all the classes have basically the same functionality and very similar parameters. It does make sense to me to make an interface and make each of the classes implement it, however there is a small hickup.
Each type of chart requires slightly different parameter set as for example piechart doesn't care about the time axis, bar chart has very distinctive time points, line chart pretends to be real time, etc.
I know that a parameter can be specified as a generic type in the interface and defined as concrete type in the implementation of the method, but I have never seen a variable number of parameters, so I created numer of overloaded methods and this got me thinking:
What is the "correct" way to do it?
like I did it (the lazy way) - define every method as overload in the interface and make unused methods throw an exception when called in a wrong class.
Make a container object and pass it (Seems like a waste of resources to pass object containing for example time series data for a pie chart)
Drop use of interface completely as it does not fit in that case perfectly and make separate classes that are just very similar.
Something else?
I would like justification of answer if you suggest one option or another.
My justification for going with 1. is that it makes the purpose very explicit, but it does seem like a violation of principles behind interface.
I have two classes, ImageMap, extending ImageView and PageView extending GLSurfaceView, I am using the ImageMap to mainly have hot spots on drawables but I also need to add a page flip/curl animation to it, in order to do that I need those two classes to act as one object, any idea how to do that?
It is totally clear to me that multiple inheritance is not allowed in java.
There is no way of really extend two classes. What you can do is:
You make a wrapper object, that holds one instance of each object. and simply do this.ImageMap.filed1 and so. This is more convenient while developing the class. This also allows you to proxy method invocations.
You define interfaces which should be implemented, and you make a new class which implements both. This is only for class that use this class to have the interface, without really caring about the implementation.
You may need both things, since the first is about "how to do it" and the second about "how it will be presented to objects that use it".
Your question is not about Android; it's about Java.
Java does not allow for multiple inheritance.
Your reasoning is inaccurate regarding the following:
in order to do that I need those two classes to act as one object
That's not the case. An 'Activity', for example, does not have to be an event handler; it's enough if your 'Activity' can have an event handler, e.g. as an inner class which can access the Activity's variables.
I know how to using Reflection determine the super class of a class. So if I had a game, and I had a map editor, and I wanted it to have a panel with a button to select select any type of Solid that can be added to the game and add it, I could theoretically use reflection to look through all the classes in my game and see which ones have the super class "Solid", and then add a button to the panel. The only thing that prevents me from doing this is that I need a list of all classes to search through, is there a way I can get that?
You probably want the Java Service Provider Interface. It gets around this problem by having you list the implementing "plug-in" (provider) classes in the jar manifest. You then use ServiceLoader to ask for all of the available implementations, and you only have to use Class.forName. Here's another useful overview.
I have created a method for getting the text from a selected radio button when I pass it the buttongroup it's in. I've been reading that helper/utility classes might not be the best idea because they can turn into god classes, which I can see happening after a while. Plus, I'm only adding one method and that method is very specific to buttons.
So my question is, is there a best way to add a method to a class that oracle made? I would like this method to always be available whenever I make a new project and use buttongroups.
Create a subclass that contains your new method.
Inheritance: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
Do you mean that you want to add methods to JRadioButton? You cannot do that. The typical way this is done is by extending JRadioButton, add the method and use that class everywhere.