Background
I have access to the source of a library I'm using for audio recording/analysis and need to modify it for my application.
There is a main activity (class A) in which I set up views and stuff. The library has a class (class B) that handles raw audio data from the microphone using Audio Recorder. An instance of that class is run as a task through another class (class C) that extends AsyncTask, of which the main activity has an instance.
So class A has an instance of class C, class C has an instance of class B and class B has the raw data I want to access from class A.
Why?
I want to call an activity that displays a graph with raw data found in class B.
What I tried
I added a method to get the raw data in class B and call it from class C. Now I could create a similar method in class C so that class A can get raw data from class C. That, however, makes me want to puke.
Another way could be to call the graphing activity from class C itself, but I feel I'm adding logic that doesn't belong to that class.
I want a nice way to implement this, but have no background in design patterns nor much common sense. Help would really be appreciated here.
/E
I think the correct OO way is, as you have described it:
add a method getRawData to class B
add a method getRecordedAudio to class C
Explanation
Even though, it may at first seems arkward, it is the correct object oriented way, because, that way you will later be able to access the raw data from class B using that method also from any other class (say class D) without the need to think up of additional logic again.
Also, it may be possible that you need to run that Async task (C) from another activity (say E). In that case you can savely reuse getRecordedAudio without the need to reinvent the wheel.
Final notes
From what you have explained about your API this is best way I can come up with. If there are any other limitations, you need to tell us.
What's about the good old Observer Pattern.
Your Class B would be the Observable that needs a method to register Observers. A and C would be both Observers that will register with B, they both have a method to be notified.
If there are any changes in the data of an instance of B, it will call the notify method for all registered objects A and C and they can react on it seperately.
I think if the sources are available that's the most elegant way to accomplish your task. Have a look on the wiki link for further information on what you would need to implement the pattern, but in general it's quite straight forward.
Related
I'm trying to execute the same code in every subclass of a class once the class is loaded. I need to scan the classes fields using reflection to generate an outline of the data container/class.
Is there any way to do this without invoking a method of the superclass in every subclass once it is initialized? (e.g. inheriting the static initializer of the super class)
EDIT: I'm writing a library that manages some data objects. When I want to store more data in them, I'd usually subclass them. But as this get's pretty annoying when handing them back to the library and having to cast, check instanceof all the time, I wanted to create a system of "data components", that could be added to this data objects. I want to be able to use these data components in code by directly accessing their members (not via id or string), serialize them and let the user edit them in a dynamic GUI. For generating the GUI (and for serializing) I need to know the fields of the "data component classes", that were handed to my library, but I don't want to force the main application to make a library call to register each new data component class. Therefore I'm asking if it's possible to do this automatically for each subclass of a given "data component" class, that is loaded. (without classpath scanning)
Another solution would be to just declare a getFields() method in the data component superclass which automatically scans/registers the subclass when needed, but this would add some delay (i don't know how fast/slow reflection is) on the first call when the application is not in init anymore and therefore should be running as fast as possible. (I'd like to do the scanning beforehand)
Notify activity from service
I want to know if it is possible to do what the selected answer in the above post said, when your activity and service are in separate packages. Basically i have an object that is non-serializable (lets say a created view) and I want to send it from my service to my activity. Would be easy enough by using a custom binder, but as i've found out, you cant use custom binders when your service and activity are in separate packages.
I've been pondering this for a few weeks and it has really put a block in my project I am working on.
For those who will ask, I am trying to make a framework that allows "plugins" from other packages. But I am unsure how one would send non-serializable date back and forth between said service and activity.
It depends on the complexity of the object, If the object that you want to serialize is an object that comes from the Android SDK lets say a RelativeLayout or a Cursor I don't see that happening anyway, because those objects contains references to another objects that you don't have access to modify or make them implement the Serializable interface.
If your object is a class that you implemented and all references inside that class are also to another classes that you implementad (or to Serializable/Parceable objects) then sure you can. One way of doing so is, well, making them implement Serializable or making your own Parceable theres plenty of tools to achive this in a quick way like this or this one.
If no one of this answers satisfies you, then tell me what're you trying to send from the Service.
EDIT
Did you try to make a class implement both Serializable and OnClickListener and send it through the intent?
Sounds like you need some sort of Command pattern there.
I have a method inside of a class called
ChopraWisdom.GetQuote()
that pulls in some data from the interwebs. To use it I have to use an AsyncTask in my activity. Thats fine and all, but my Activity code now gets cluttered with the AsyncTask code.
I was hoping that I could hide the AsyncTask in my Class and then create another method called
ChopraWisdom.GetQuoteAsync()
But I'm not sure how to pass in "Actions" (I come from a .Net background) in order to handle the various UI updating that needs to take place.
In C# I would define the method signature as something like:
ChopraWisdom.GetQuoteAsync(Action preExecute, Action postExecute, Action updateProgress)
Questions:
Does java have something comparable to 'Action'?
What is the acceptable pattern for 'wrapping' Async functionality like this in Java?
I like clean code, but am I being to picky here?
Please provide examples.
EDIT - Added Example class
public class ChopraWisdom
{
public string GetQuote()
{
//...do stuff and return a string
}
}
You should really think about using Loaders instead of AsynkTask(with android support lib).
If you still want to use AsyncTask in your situation best way would be to create new interface Action(something like https://github.com/ReactiveX/RxJava/blob/1.x/src/main/java/rx/functions/Action0.java)
You could use RxJava in your project and use all they have https://github.com/ReactiveX/RxJava/tree/1.x/src/main/java/rx/functions
You could use https://github.com/evant/gradle-retrolambda in combination with (2) option to provide C# like lambdas in your java code.
Java does have something comparable to Action. It is called Function and only available in Java 8. The standard way for passing a function as parameter is to create an interface and provide it as a parameter. That way you can either pass in an instance of a class implementing that interface or create an anonymous class inline. You encounter the latter everywhere in Android (OnClickListener, etc ...)
I would highly recommend you to take a look at Android Annotations. It provides useful features like:
Dependency injection
View injection
OnClickListener via annotation
AsyncTask via annotation
...
And the best thing: everything is done at compile time through subclassing, therefore there is no performance impact and you can check what the framework is doing at any given point.
You are not too picky at all. Clean code is very important in Android development as you have to write a lot of boilerplate- / gluecode anyway.
Here are some other handy android frameworks that are definitely worth checking out:
GreenDao
Eventbus
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 am trying to implement a custom Java widget using GWT.This requires me to copy a class from the GWT API and pasting it in my own new Class.(I am not sure if this is a right approach.Suggest me if its wrong to copy the API in my new class).The reason why i am doing this is i need to make modifications to the API,because the API does not provide me getter/setter's for a object.
But the problem with this is ,the API class uses many methods which have the protected access modifier.So when i paste this code in my package ,these methods are not recognized.I cannot even think of making my class a sub class (a workaround for protected access modifier) as the methods are from different classes and i cannot make my class a sub class of more than one class.
Can any one suggest me a work around for this scenario.I am trying to implement a widget whose functionality is similar to the browser's navigation widget(the place where we enter a website's url).Its similar to combining the functionality of ListBox+SuggestBox.
This is my previous question.That is what i am trying to implement.
Thanks
Derive a new widget by extending from Composite and then implement whatever functionality you need within that. e.g. If you need a ListBox to make suggestions then create one from inside your Composite and hook up whatever listeners you need to on the inner widget to drive suggestions.
A sample of a Composite widget is shown here.
There shouldn't be any reason to have to copy & paste existing source code. Indeed doing so is not going to get you far since most widgets in GWT are just wrappers HTML elements anyway with some plumbing to hook up to the event model.
It would really help if you provided some sample code, you should never have to copy and paste code from the API. What are you trying to extend and what variables within that class do you need access to and to do what? Usually there is a reason why variables are private and thats because messing with them will cause a break in functionality.