Java:'world' 对象中的 'agent' 对象如何获取有关 'world' 的信息?

Java: How can an 'agent' object that is inside a 'world' object obtain information about the 'world'?

很抱歉这个问题的措辞有点疯狂,但我对面向代理的思维很陌生(这些是 'patterns' 吗?)而且对 java 只是稍微不那么陌生,而且我我正在为一个非常基本的问题而苦苦挣扎。

我正在做很多这样的事情 'intuitively'(即盲目地),而不是试图理解别人的代码 - 部分原因是我很难理解 'above my level' 的代码,但也因为我希望这样做 'my way' 会帮助我以后正确地理解。

基本上,我是在环境(房子)中对代理(机器人吸尘器)建模。房屋包含房间的集合 (HashMap)。 House 有一个方法 getRoom(int key) returns 一个 Room 匹配给定的键。 Agent 有一个 State,此时跟踪一个房间 ID(这也是 House 中的键),这是机器人所在的房间 'in' 用于导航世界; a State 还描述了房间是否已知是脏的。构建代理时,会使用 ID 对其进行初始化(必须创建 'in a room'),但不会为其指定 Room 的 dirty/clean 状态。我希望代理检查污垢 - 这将涉及调用 House 中的 getRoom() 方法。但是,根据我到目前为止所学的 Java,我不知道该怎么做。我知道我可以通过在 java 中创建一个房屋或通过将方法设为静态来访问该方法,但这些都行不通 - 代理需要了解内存中的特定房屋,即具有已使用 Rooms 进行初始化。

tl;dr:Agent 对象如何获取对存储在另一个 Environment 对象内的 HashMap 中的对象的引用?

P.S 这是我想象的通过这种方法实现的 'higher level' 视角模型: 我有点直觉地希望 Agent 对自己的规则和行为完全负责,这样上面的代码看起来更像:

agent.percieve()                           //agent checks the room it thinks its in for dirt and exits
if (agent.gotDirt()) agent.clean()         //agent activates its cleaning device if it found dirt in this room
if (agent.gotDirt() == false) agent.move() //agent picks an exit and leaves the room

真空吸尘器(即你所说的 "Agent",但为什么要这样命名,因为它实际上是一个真空吸尘器?)只需要引用它所属的 House 对象:

// a single House is constructed
House house = new House(); 
// omitted: add rooms to the house...

// create a first vacuum cleaner for the house. A reference to the house is given to this cleaner
VacuumCleaner vacuumCleaner = new VacuumCleaner(house);
System.out(vacuumCleaner.isRoomClean(2)); // prints false, because room 2 is full of dirt
vacuumCleaner.cleanRoom(2);
System.out(vacuumCleaner.isRoomClean(2)); // prints true, because the vacuum cleaner removed the dirt from room 2

// now let's create a second vacuum cleaner for the same house
VacuumCleaner vacuumCleaner2 = new VacuumCleaner(house);
System.out(vacuumCleaner2.isRoomClean(2)); // prints true, because room 2 has no dirt: it has previously been removed from the room by the first vacuum cleaner.

编辑

VacuumCleaner class 的外观如下:

public class VacuumCleaner
    /**
     * The house that this vacuum cleaner cleans 
     */
    private House house;

    public VacuumCleaner(House houseToClean) {
        this.house = houseToClean;
    }

    public boolean isRoomDirty(int roomId) {
        // find the room in the house, and see if it contains dirt
    }

    public void cleanRoom(int roomId) {
        // find the room in the house, and remove dirt from it
    }
}