Sign in to follow this  

Storing methods in an array

This topic is 1956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This 100-something methods sounds like a bit cf code smell... especially that you want to store them in arrays.

In Java, you have anonymous inner classes. You can emulate it with function interfaces. Function interfaces are interfaces that have just a single method and are for callbacks. E.g.

[code]a
public interface MyCallback {
void doStuff(int someArg);
}
[/code]

Then, suppose you want to call your methods. If they have the same signature, it is trivial - the classes implement the interface, and you can store them in the array. Otherwise, you would use anonymous inner classes, something like

[code]
public class Impl1 {
public boolean goodMorning();
...
}

...

Impl1 instance = new Impl1(...);
List<MyCallback> arr = new ArrayList<MyCallback>();
arr.add(new MyCallback() {
public int doStuff(int someArg) {
instance.goodMorning();
});


[/code]
And, obviously ,you will just use the interface to call the methods.

Share this post


Link to post
Share on other sites
I don't understand how this could put the methods into an array that i can call whenever a certain card is played or if the card is drawn from the deck. maybe I need to do a little more learning before I start this project

Share this post


Link to post
Share on other sites
The basic idea is that you don't have function pointers/delegates in Java. So you write an interface that has only one method. Java has anonymous classes, and so you can implement the interface on the spot. What happens is that the anonymous class wraps the call to the original method in its interface implementation. In the end, you have an instance of the anonymous class, which implements the one-method-interface (in Java they are called function objects). Since you know what your interface is, you can call the interface method, which in turn calls the wrapped method. And since you can have arrays of anything (btw in Java, collections are generally preferred to arrays for almost anything, the compiler/JVM would optimize it anyway).

Oracle has a set of *very* good [url="http://docs.oracle.com/javase/tutorial/index.html"]tutorials on Java[/url]. They are some of the best programming tutorials I've ever read. They have a [url="http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html"]tutorial on inner classes[/url], which explains the basics. If you are ever stuck on anything, they should be your starting point.

Share this post


Link to post
Share on other sites
I think if you work on some smaller/simpler games before you dive into the card game you're designing (I recall you mentioning it in another thread), you will find a better way to implement this than writing a method per card and trying to put those methods into a container.

Share this post


Link to post
Share on other sites

This topic is 1956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this