简体   繁体   English

Java:访问在别处创建的 object

[英]Java: accessing object created elsewhere

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:考虑以下 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 Box es created later, I would have to construct an instance of it and then call the getter method to access the list.据我了解,为了将这个 class 用作稍后创建的Box es 的容器,我必须构造它的一个实例,然后调用 getter 方法来访问列表。 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 .从哪里我可以使用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.问题 1:据我所知,为了调用 class 的私有字段,需要有问题的 class 的实例。 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)?如果我坚持保持拥有多个Container实例的可能性,为了访问上面在不同类/方法中创建的newList ,我是否必须继续将它作为参数传递给任何调用它的方法(会有很多) ? Of course, a method in another class does not know what newList is.当然另一个class中的一个方法不知道newList是什么。

Question 2: If only one Container is ever intended to exist, would it be better to just replace问题 2:如果只打算存在一个Container ,最好只替换

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?然后去掉构造函数,声明Container static的所有方法,直接调用Container.getBoxList()

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?问题 3: class 是否有通常的通用设置,仅用于存储对象并处理它们(添加到列表、删除等),例如此处的Container How is the communication of such class with other classes usually implemented?这种 class 与其他类的通信通常是如何实现的?

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.我认为您正在尝试做的事情称为 Singleton 设计模式 - 应该只存在一个 object 实例的想法,并且应该可以全局访问。

The main way this works is essentially, attach an instance of the class to the class as a static attribute:其工作的主要方式本质上是将 class 的实例附加到 class 作为 static 属性:

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.现在,在整个程序中,您需要做的就是导入Container class 并调用container.getInstance() ,您就拥有了一个所有人共享的容器。


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.如果调用者不想要 singleton 容器,他们可以以通常的方式实例化自己的容器。 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.如果您希望集中访问多个实例,您可以将private static Container instance替换为ListMap ,并向getInstance()添加参数以允许调用者选择他们想要的实例。

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, ie in getInstance() ).或者,如果您只想在整个程序中强制执行一个实例,您可以将构造函数设为private (以便它只能在 class 内部调用,即在getInstance()中)。

You can also extend the design pattern in myriad other ways according to your specific needs.您还可以根据您的特定需求以多种其他方式扩展设计模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM