|
-
Nov 6th, 2011, 07:55 AM
#1
"Delegates" in Java? Passing methods
Hi,
I'm kinda new to Java but was getting along fine until now.
I'm working on an Android game using the Android SDK, but I think my question is purely Java related, so I don't think that's important.
What I want to do is quite simple but might be hard to explain. I want to be able to pass a "method" as a parameter into a class, and invoke or call that method at a later time.
I have a class that loads some data over the internet, which might take a few seconds. Obviously I am loading the data asynchronously in a separate thread to not lock op the UI during that time. Android has a couple useful classes just for that thing, but they are a little tedious to use, so I'm looking for another way.
The ideal solution I am looking for would be a class that looks like this:
Code:
public class GetDataTask<E> extends (some Android class)
{
private ???<Void, E> getDataMethod;
private ???<E, Void> finishedMethod;
public GetDataTask(??? method1, ??? method2)
{
getDataMethod = method1;
finishedMethod = method2;
}
@Override
public E doInBackground()
{
// This method is called by the superclass and runs in a background thread
// call the method passed as the getDataMethod
return getDataMethod.invoke();
}
@Override
public void onPostExecute(E value)
{
// This method is called by superclass as soon as the background thread method has completed
// call the finished method
finishedMethod.invoke(value);
}
}
I want to pass two methods:
- getDataMethod is a method that returns an object of type E (the generic type of the class)
- finishedMethod is a method that accepts an object of type E.
Usage would then be very simple, for example:
Code:
public void onSomeButtonClicked()
{
// Get user details:
GetDataTask<User> task = new GetDataTask<User>(loadUser, loadUserFinished);
task.execute();
}
public User loadUser()
{
return Networking.getUserFromNetwork();
}
public void loadUserFinished(User user)
{
txtUserName.setText(user.username);
}
The loadUser and loadUserFinished methods are the two methods I pass along to my GetDataTask.
I have no clue what to use as the ??? types. I am now thinking in .NET terms where these methods could be passed as Action<T>, Func<Tin, Tout>, etc. As far as I know, there is nothing like this in Java.
One option I thought of was that the ??? types would be an Interface that my main window (Activity actually in Android) has to implement.
For example, in this case:
Code:
public interface IDataLoader<E1, E2>
{
E1 getData(E2);
E2 getDataFinished(E1);
}
Then I can let my window implement IDataLoader<User, Void> and it would have the getData and getDataFinished methods. Instead of passing these two methods, I pass the entire window (as an IDataLoader reference) and I can then call these methods on it.
There is one major problem here though: it works fine if I just have one of this task, but if I have another task, let's say one that returns Messages sent by the user, then I have to implement the same interface for that task. That apparently is impossible in Java, I cannot implement the same interface twice with different type arguments. So that doesn't seem like an option...
So what are my options?
Another usage I can think of is using anonymous inner classes (I think), but I have no clue how I would implement that in my GetDataTask:
Code:
public void onSomeButtonClicked()
{
// Get user details:
GetDataTask<User> task = new GetDataTask<User>(loadUser, loadUserFinished);
task.execute();
}
private loadUser = new ??? ()
{
public User onLoad()
{
return Networking.getUserFromNetwork();
}
}
private loadUserFinished = new ??? ()
{
public void onFinished(User user)
{
// Get reference to the textbox, dunno how
txtUserName.setText(user.username);
}
}
Again, a couple of ??? types where I don't know what to use. Also, as far as I can see I now have no access to the txtUserName, because this onFinished method in the loadUserFinished 'field' (what is it? inner type?) doesn't know about the class it's in.. right?
So what are my options, how can I do what I want to do here?
Thanks for any help!
Last edited by NickThissen; Nov 6th, 2011 at 07:59 AM.
-
Nov 8th, 2011, 03:56 PM
#2
Re: "Delegates" in Java? Passing methods
I still find it hard to believe that Java doesn't have delegates.
You Java developers have got to start pressuring Oracle or the Java standards committee to keep the language evolving. It's one thing to be sparse and all, but no delegates? Sheesh.
The latest C++ standard has more new features than the entire Java language has had since it's inception - not that you want Java to become as bloated as C++, but ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|