Android: determining if code should reside in activity or custom view

I think your version 3 is really better than the 2 others: your model (i.e. World) is independent from your your view (MapView) and your controller (GameActivity) bind them together.

I think you can improve the way your World object is created using the Builder pattern so that the job to create it is in a separate class. Let me show you what I mean :

public class WorldBuilder {

    private File worldFile;
    private String name = "default_world";
    private int width = 1;
    private int height = 1;

    public static WorldBuilder fromFile(File worldFile){
        WorldBuilder worldBuilder = new WorldBuilder();
        worldBuilder.worldFile = worldFile;
        return worldBuilder;
    }

    public WorldBuilder withName(String name){
        this.name= name;
        return this;
    }

    public WorldBuilder withWidth(int width){
        this.parameter2 = param2;
        return this;
    }

    public WorldBuilder withHeight(int height){
        this.height = height;
        return this;
    }

    public World build(){
        World world = new World(name,width,height);
        if(worldFile!=null)
            world.loadWorld(worldFile);
        return world;
    }    
}

In GameActivity, you can create the world with this single line of code:

World world = WorldBuilder.fromFile(worldFile)
        .withName(p1)
        .withWidth(p2)
        .withHeight(p3)
        .build();

And if you need to create a world with the default parameters you can simply write :

World world = WorldBuilder.fromFile(null).build();

EDIT

Where writing the code ?

All computation code than relies only on World data can be written in the World class. Never pass the MapView as an argument of a World method (keep the model independent from the view).

Try, as much as possible, to organize your code in such a way that computation is not done in MapView. The MapView must only contains code directly related to display.

Tags:

Android