Skip to content
Advertisement

Conserving RAM in java by using references to objects

I know some about java, and a lot about the lower level functions of a computer, so I’m always looking for ways to conserve ram. I’m also a fan of dwarf fortress, so I’ve been trying to get some similar system working. So, what I want to do is to make multiple tiles, assign them some type of material, and all the tiles with the same type of material share the same material object. I drew a little picture to illustrate it: here Does java do this automatically? or do I have to implement it by myself? If I make two copies of the material properties, is there a way to merge them both into one?

Thank you for responding.

Advertisement

Answer

Lets say you have a material.

interface Material{
    void render( Context c);
}

Then you have some instance of the material.

class Concrete implements Material{
    static Texture concreteTexture = loadConcreteTexture();
    Texture texture;
    public Concrete(){
      texture = concreteTexture();
    }
    void render(Context c){
        //do stuff with texture.
    }
}

In this case there would be 1 concrete texture for all of the instances of concrete, and it gets loaded when the class gets loaded. The texture gets assigned in the constructor, but you might use a factory method instead that would create new materials and load/assign the assets.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement