How could you call a method from all instances of the class?

I am writing a basic game engine and have an abstract class that represents any object that can be drawn in the 3D world, however, inside this class there is an abstract Render () method, which I would automatically call the engine on each. How could I implement this so that every class that extends from my abstract class automatically calls Render ()?

I am using java, android sdk 2.2 and opengl es.

+5
source share
3 answers

You can register every object that can be passed to a class that will call render () for all of your objects.

For instance:

public class Registry{
    private static Collection<RenderedObject> register = new ArrayList<RenderedObject>();

    public static void add(RenderedObject obj){
        register.add(obj);
    }

    public static void renderAll(){
        for(RenderedObject obj : register){
            obj.render();
        }
    }
}

RenderedObject.

+3

Proxy :

public class RenderingEngine implements Engine {
    private Engine originalEngine;
    private Collection<AbstractRender3DObject> items;

    public RenderingEngine(Engine originalEngine) {
      // assing
    }
    public void draw() {
         originalEngine.draw();
         invokeRender();
    }

    private void invokeRender() {
       for (AbstractRenderItem item : items) {
           item.render();
       }
    }

    public void register(Object3D item) {
         if (item instanceof AbstractRender3DObject) {
             items.add(item);
         }
         super.register(item);
    }
}

, , . , .

+1

Maintain a list of all objects, scroll through them and call the method.

0
source

All Articles