开发者

How to use callbacks in my own code

开发者 https://www.devze.com 2022-12-21 00:44 出处:网络
Here I update my world to include the canvas开发者_StackOverflow中文版 size world.getViewPort().updateViewPortSize(width,height);

Here I update my world to include the canvas开发者_StackOverflow中文版 size

world.getViewPort().updateViewPortSize(width,height);

Is there a better way to do this? Is there a way that I can automatically update my world object without having to manually call it in the setSurfaceSize method but instead call it from My world class?

My guess is that I can use some sort of callback, but I don't understand them!

/* Callback invoked when the surface dimensions change. */
public void setSurfaceSize(int width, int height) {
    // synchronized to make sure these all change atomically
    synchronized (mSurfaceHolder) {
        mCanvasWidth = width;
        mCanvasHeight = height;
        world.getViewPort().updateViewPortSize(width,height);
    }
}


Callback functions are pretty straightforward in Java. The way to do it is to simply define some interface A that has a single method m(), then add a collection of type A to your World class. Then, at some point in your World object you want to iterate over your A collection and call the method m() on each element. To add "callback functions" to your World, you then simply define some class that implements your interface A and put it in the collection of A objects in your World class. For example,

public interface CallbackFunction
{
    public void execute(int a, int b);
}

public class World
{
    List<CallbackFunction> callbackFunctions;

    public void addCallback(CallbackFunction f)
    {
        callbackFunctions.add(f);
    }

    private void updateWorld()
    {
        // This loop could be anywhere in your World class
        for(CallbackFunction f : callbackFunctions)
        {
            f.execute(<some int>, <some int>);
        }
    }
}

Then to add a function to your World:

world.addCallback(new CallbackFunction()
{
    public void execute(int a, int b)
    {
       // Do some stuff
    }
}

I leave the rest up to you, but this should give you a good idea of how callbacks are typically implemented in Java.

You don't need to store a general collection of callback functions either, you could have specific callback functions coupled with particular methods in your World class. For instance, your World class may have some method that sorts things by comparing two elements in a collection. To determine if one object should go before or after another, you could define a CallbackFunction interface that has a method that takes 2 things (like int's) and returns a value that indicates which thing should go first. For example,

public interface CallbackForSorting
{
    public int compare(int a, int b);
}


public class World
{
    List<Integer> thingsToSort;

    public void sortThings(CallbackForSorter sorter)
    {
        int result = sorter.compare(thingsToSort.get(i), thingsToSort.get(i+1));

        if(result == 0)  // elements are equal
        if(result == -1) // thingsToSort.get(i) is "less than" thingsToSort.get(i+1)
        if(result == 1)  // thingsToSort.get(i) is "greater than" thingsToSort.get(i+1)
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消