Java: accessing object created elsewhere

Tags: ,



Three questions related to each other here. I’ve tried looking for answer, but I’m unable to find/apply possible existing answers to my problem. Consider the following class:

import java.util.ArrayList;
public class Container
{
    private ArrayList<Box> boxList = null;

    public Container()
    {
        this.infoList = new ArrayList<Box>();
    }

    public ArrayList<Box> getBoxList()
    {
        return this.boxList;
    }

To my understanding, in order to use this class as a container for Boxes created later, I would have to construct an instance of it and then call the getter method to access the list. For example,

Container newContainer = new Container();
ArrayList<Box> list = newContainer.getBoxList();
... // Creating boxes and their contents
Box box = list.get(0); // First box in list
Item item = box.getItem(); // etc.

from where I can work with item.

Question 1: As far as I know, in order to call a private field of a class an instance of the class in question is necessary. If I insist on keeping the possibility of having multiple instances of Container, in order to access newList created above in a different class/method, do I have to keep passing it as a parameter to whatever method ever calls it (there would be many)? Of course, a method in another class does not know what newList is.

Question 2: If only one Container is ever intended to exist, would it be better to just replace

private ArrayList<Box> boxList = null;

with

private static ArrayList<Box> boxList = new ArrayList<Box>;

then remove the constructor and declare all methods of Container static, and call Container.getBoxList() directly?

Question 3: Is there a usual general setup for a class intended to only store objects and handle them (add to list, remove etc.), such as Container here? How is the communication of such class with other classes usually implemented?

Answer

I think what you’re trying to do is called the Singleton design pattern – the idea that only one instance of an object should exist, and it should be accessible globally.

The main way this works is essentially, attach an instance of the class to the class as a static attribute:

public class Container
{
    private static Container instance = null;
    public static Container getInstance() {
        // create a singleton instance if one does not already exist
        if (instance == null) {
            instance = new Container();
        }
        // return the existing global instance
        return instance;
    }
    ...
}

or, even more simply:

public class Container
{
    private static Container instance = new Container();
    public static Container getInstance() {
        return instance;
    }
    ...
}

Now, all throughout your program, all you need to do is import the Container class and call container.getInstance(), and you have the one single container that everyone shares.


If you want it to be possible for multiple instances to exist, you can keep the constructor public, as it is currently. If a caller doesn’t want the singleton Container, they can just instantiate their own in the usual way. If you want multiple instances to be centrally accessible, you can maybe replace the private static Container instance with a List or Map, and add parameters to getInstance() to allow callers to choose which instance they want.

Alternatively, if you want to enforce only one instance throughout the program, you can make the constructor private (so that it can only be called inside the class, i.e. in getInstance()).

You can also extend the design pattern in myriad other ways according to your specific needs.



Source: stackoverflow